1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include<pthread.h> #include<stdio.h> #include<stdlib.h>
#define NUM_OF_TASKS 3 #define MAX_TASK_QUEUE 11
char tasklist[MAX_TASK_QUEUE]="ABCDEFGHIJ"; int head = 0; int tail = 0;
int quit = 0;
pthread_mutex_t g_task_lock; pthread_cond_t g_task_cv;
void *coder(void *notused){ pthread_t tid = pthread_self();
while(!quit){ pthread_mutex_lock(&g_task_lock);
while(tail == head){ if(quit){ pthread_mutex_unlock(&g_task_lock); pthread_exit((void *)0); } printf("no task now! thread %u is waiting! \n", (unsigned int)tid); pthread_cond_wait(&g_task_cv, &g_task_lock); printf("have task now! thread %u is grabing thw task !\n", (unsigned int)tid); } char task = tasklist[head++]; pthread_mutex_unlock(&g_task_lock); printf("thread %u has a task %c now! \n", (unsigned int)tid, task); sleep(5); printf("thread %u finish the task %c ! \n", (unsigned int)tid, task); } pthread_exit((void *)0); }
int main(int argc, char *argv[]){ pthread_t threads[NUM_OF_TASKS];
int rc; int t;
pthread_mutex_init(&g_task_lock,NULL); pthread_cond_init(&g_task_cv,NULL);
for(t =0 ; t < NUM_OF_TASKS; t++){ rc = pthread_create(&threads[t], NULL, coder, NULL); if(rc){ printf("ERROR , return from pthread_create %d \n" , rc); exit(-1); } }
sleep(10);
for (t =1 ; t <= 4 ; t++){ pthread_mutex_lock(&g_task_lock); tail += t; printf("I am Boss , I assigned %d tasks, I notify all coder! \n", t); pthread_cond_broadcast(&g_task_cv); pthread_mutex_unlock(&g_task_lock); sleep(20); }
while(head != tail ){ } pthread_mutex_lock(&g_task_lock); quit = 1; pthread_cond_broadcast(&g_task_cv); pthread_mutex_unlock(&g_task_lock);
pthread_mutex_destroy(&g_task_lock); pthread_cond_destroy(&g_task_cv); pthread_exit(NULL);
}
|