1 #include <err.h> 2 #include <pthread.h> 3 #include <pthread_np.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 static void * 9 setselfname(void *arg __unused) 10 { 11 pthread_set_name_np(pthread_self(), __func__); 12 pause(); 13 return NULL; 14 } 15 16 static void * 17 waitname(void *arg __unused) 18 { 19 pause(); 20 return NULL; 21 } 22 23 static void * 24 resetname(void *arg __unused) 25 { 26 pthread_set_name_np(pthread_self(), __func__); 27 sleep(10); 28 pthread_set_name_np(pthread_self(), NULL); 29 pause(); 30 return NULL; 31 } 32 33 int 34 main(void) 35 { 36 pthread_t tid1, tid2, tid3; 37 char longname[256]; 38 int error; 39 40 error = pthread_create(&tid1, NULL, setselfname, NULL); 41 if (error) 42 errc(1, error, "pthread_create(setselfname) failed"); 43 44 error = pthread_create(&tid2, NULL, waitname, NULL); 45 if (error) 46 errc(1, error, "pthread_create(waitname) failed"); 47 pthread_set_name_np(tid2, "waitname"); 48 49 error = pthread_create(&tid3, NULL, resetname, NULL); 50 if (error) 51 errc(1, error, "pthread_create(resetname) failed"); 52 53 memset(longname, 'x', sizeof(longname)); 54 longname[sizeof(longname) - 1] = '\0'; 55 pthread_set_name_np(pthread_self(), longname); 56 57 pause(); 58 exit(0); 59 } 60