当前位置: 技术问答>linux和unix
关于条件变量的问题,求解答
来源: 互联网 发布时间:2017-04-03
本文导语: #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); ...
#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