当前位置: 技术问答>linux和unix
大家帮忙看看这个Linux程序为什么有问题,关于条件变量的!!
来源: 互联网 发布时间:2015-12-11
本文导语: 主线程pthread_cond_signal,其他线程pthread_cond_wait,但是pthread_cond_wait不能被唤醒. #include #include #include #include #include #include #include #include #include #include #include #include #include #include pthread_mutex_t pool_mutex; p...
主线程pthread_cond_signal,其他线程pthread_cond_wait,但是pthread_cond_wait不能被唤醒.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
pthread_mutex_t pool_mutex;
pthread_cond_t pool_cond;
#define INITSIZE 50
pthread_t tid[INITSIZE];
void *thrmgr_worker(void *arg)
{
/* loop looking for work */
for (;;)
{
if (pthread_mutex_lock(&(pool_mutex)) != 0)
{
printf("!Fatal: mutex lock failedn");
exit(-2);
}
printf("before waitingn");
printf("%dn",pthread_cond_wait(&pool_cond,&pool_mutex));
//注意这儿,pthread_cond_wait阻塞了,pthread_cond_signal不能唤醒它,为什么
printf("after waitingn");
if (pthread_mutex_unlock(&pool_mutex) != 0)
{
printf("!Fatal: mutex unlock failedn");
exit(-2);
}
}
return NULL;
}
int serverinit()
{
int i = 0;
for (; i
pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t pool_cond; =>
pthread_cond_t pool_cond = PTHREAD_COND_INITIALIZER;
3, 在pthread_cond_t pool_cond;下面增加变量:
int flag;
4, 将for (;;)改为while (flag == 0)
/* loop looking for work */
for (;;) => while (flag == 0)
5, 在int serverinit()函数里 注释掉
pthread_mutex_init (&pool_mutex, NULL);
pthread_cond_init (&pool_cond, NULL);
6, if (pthread_mutex_unlock(&pool_mutex) != 0)
{
printf("!Fatal: mutex unlock failedn");
exit(-2);
}
这段代码放到while 循环外(while 是把以前的for修改成while)
7, 在printf("after waitingn");后加上 flag = 1;
8,在main函数里的pthread_cond_signal(&pool_cond);前加上一句 : flag = 1;
diff 的结果:
15a16
> #include
17,18c18,20
pthread_cond_t pool_cond = PTHREAD_COND_INITIALIZER;
> int flag;
27c29
while (flag == 0)
38a41,42
> flag = 1;
> #if 0
44a49,55
> #endif
> }
> if (pthread_mutex_unlock(&pool_mutex) != 0)
> {
>
> printf("!Fatal: mutex unlock failedn");
> exit(-2);
60,61c71,72
//pthread_cond_init (&pool_cond, NULL);
80a92
> flag = 1;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
pthread_mutex_t pool_mutex;
pthread_cond_t pool_cond;
#define INITSIZE 50
pthread_t tid[INITSIZE];
void *thrmgr_worker(void *arg)
{
/* loop looking for work */
for (;;)
{
if (pthread_mutex_lock(&(pool_mutex)) != 0)
{
printf("!Fatal: mutex lock failedn");
exit(-2);
}
printf("before waitingn");
printf("%dn",pthread_cond_wait(&pool_cond,&pool_mutex));
//注意这儿,pthread_cond_wait阻塞了,pthread_cond_signal不能唤醒它,为什么
printf("after waitingn");
if (pthread_mutex_unlock(&pool_mutex) != 0)
{
printf("!Fatal: mutex unlock failedn");
exit(-2);
}
}
return NULL;
}
int serverinit()
{
int i = 0;
for (; i
pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t pool_cond; =>
pthread_cond_t pool_cond = PTHREAD_COND_INITIALIZER;
3, 在pthread_cond_t pool_cond;下面增加变量:
int flag;
4, 将for (;;)改为while (flag == 0)
/* loop looking for work */
for (;;) => while (flag == 0)
5, 在int serverinit()函数里 注释掉
pthread_mutex_init (&pool_mutex, NULL);
pthread_cond_init (&pool_cond, NULL);
6, if (pthread_mutex_unlock(&pool_mutex) != 0)
{
printf("!Fatal: mutex unlock failedn");
exit(-2);
}
这段代码放到while 循环外(while 是把以前的for修改成while)
7, 在printf("after waitingn");后加上 flag = 1;
8,在main函数里的pthread_cond_signal(&pool_cond);前加上一句 : flag = 1;
diff 的结果:
15a16
> #include
17,18c18,20
pthread_cond_t pool_cond = PTHREAD_COND_INITIALIZER;
> int flag;
27c29
while (flag == 0)
38a41,42
> flag = 1;
> #if 0
44a49,55
> #endif
> }
> if (pthread_mutex_unlock(&pool_mutex) != 0)
> {
>
> printf("!Fatal: mutex unlock failedn");
> exit(-2);
60,61c71,72
//pthread_cond_init (&pool_cond, NULL);
80a92
> flag = 1;