#define _REENTRANT #include #include /* Function prototypes for thread routines */ void *my_sub(void *); thread_t thr_a, thr_b, thr_c; mutex_t my_mutex; void main() { thread_t main_thr; int flag; main_thr = thr_self(); fprintf(stderr,"Main thread = %d\n", main_thr); if (thr_create(NULL, 0, my_sub, NULL, THR_SUSPENDED, &thr_b)) { fprintf(stderr,"Can't create thr_b\n"); exit(1); } if (thr_create(NULL, 0, my_sub, NULL, THR_SUSPENDED, &thr_a)) { fprintf(stderr,"Can't create thr_a\n"); exit(1); } if (thr_create(NULL, 0, my_sub, NULL, THR_SUSPENDED, &thr_c)) { fprintf(stderr,"Can't create thr_c\n"); exit(1); } fprintf(stderr,"Main Created threads A:%d B:%d C:%d\n", thr_a, thr_b, thr_c); mutex_init(&my_mutex,USYNC_THREAD,NULL); thr_continue(thr_a); thr_continue(thr_b); thr_continue(thr_c); fprintf(stderr,"Main Thread exiting...\n"); thr_exit((void *)main_thr); } void *my_sub(void *arg) { int which_thread; mutex_lock(&my_mutex); which_thread = thr_self(); fprintf(stderr,"Thread %d has locked the mutex.\n",which_thread); mutex_unlock(&my_mutex); fprintf(stderr,"Thread %d is exiting.\n",which_thread); thr_exit((void*)0); }