1 /* Test whether assigning names to threads works properly. */
8 #include "../../drd/drd.h"
11 #define NUM_THREADS 10
14 static pthread_barrier_t s_barrier
;
15 static pthread_mutex_t s_mutex
;
16 static pthread_cond_t s_cond
;
19 static void* thread_func(void* argp
)
21 const int thread_num
= (ptrdiff_t)(argp
);
22 pthread_mutex_t invalid_mutex
;
25 snprintf(thread_name
, sizeof(thread_name
),
26 "thread_func instance %d", thread_num
+ 1);
27 ANNOTATE_THREAD_NAME(thread_name
);
29 memset(&invalid_mutex
, 0xff, sizeof(invalid_mutex
));
31 pthread_barrier_wait(&s_barrier
);
33 pthread_mutex_lock(&s_mutex
);
34 while (s_counter
!= thread_num
)
35 pthread_cond_wait(&s_cond
, &s_mutex
);
36 fprintf(stderr
, "\n%s\n\n", thread_name
);
37 pthread_mutex_unlock(&invalid_mutex
);
39 pthread_cond_broadcast(&s_cond
);
40 pthread_mutex_unlock(&s_mutex
);
46 int main(int arg
, char** argv
)
49 pthread_t tid
[NUM_THREADS
];
51 pthread_barrier_init(&s_barrier
, NULL
, NUM_THREADS
);
52 pthread_mutex_init(&s_mutex
, 0);
53 pthread_cond_init(&s_cond
, 0);
55 for (i
= 0; i
< NUM_THREADS
; i
++)
56 pthread_create(&tid
[i
], 0, thread_func
, (void*)(ptrdiff_t)i
);
58 for (i
= 0; i
< NUM_THREADS
; i
++)
59 pthread_join(tid
[i
], 0);
61 pthread_cond_destroy(&s_cond
);
62 pthread_mutex_destroy(&s_mutex
);