日期:2014-05-16 浏览次数:20716 次
#define PTHREAD_NUM 10
pthread_mutex_t lock;
pthread_cond_t cond;
void pthread_function(void *data)
{
pthread_detach(pthread_self());
pthread_mutex_lock(&lock);
if((int )data != PTHREAD_NUM - 1){
pthread_cond_wait(&cond,&lock);
}
// sleep(1);
printf("this thread id is %u!\n",pthread_self());
pthread_cond_signal(&cond);
// pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&lock);
}
int main()
{
pthread_mutex_init(&lock,0);
pthread_cond_init(&cond,0);
int i = 0;
pthread_t thread[PTHREAD_NUM] = {0};
for(i = 0;i < PTHREAD_NUM;i++){
pthread_create(&thread[i],0,pthread_function,(void *)i);
printf("%d---%lu\n",i,thread[i]);
}
printf("\n");
sleep(20);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&lock);
return 0;
}