当前位置: 技术问答>linux和unix
线程广播信号,pthread_cond_wait却仍在等待。
来源: 互联网 发布时间:2015-08-05
本文导语: 我刚开始看线程,大家帮我看看下面的程序有什么问题。在第一个pthread_cond_wait那里就卡住了 #include #include #include pthread_mutex_t mymutex; pthread_cond_t mycond; void *thread_function1(void *arg) { printf("1st: broad ...
我刚开始看线程,大家帮我看看下面的程序有什么问题。在第一个pthread_cond_wait那里就卡住了
#include
#include
#include
pthread_mutex_t mymutex;
pthread_cond_t mycond;
void *thread_function1(void *arg) {
printf("1st: broad cast myondn");
pthread_cond_broadcast(&mycond);
return NULL;
}
void *thread_function2(void *arg) {
sleep(2);
printf("2st: broad cast myondn");
pthread_cond_broadcast(&mycond);
return NULL;
}
int main(int argc, char **argv) {
int ret;
pthread_t mythread1;
pthread_t mythread2;
pthread_mutex_init(&mymutex, NULL);
pthread_cond_init(&mycond, NULL);
if (pthread_create(&mythread1, NULL, thread_function1, NULL)) {
printf("create thread1 errorn");
}
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&mycond, &mymutex);
pthread_mutex_unlock(&mymutex);
printf("wait1n");
if (pthread_create(&mythread2, NULL, thread_function2, NULL)) {
printf("create thread2 errorn");
}
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&mycond, &mymutex);
pthread_mutex_unlock(&mymutex);
printf("wait2n");
return 0;
}
#include
#include
#include
pthread_mutex_t mymutex;
pthread_cond_t mycond;
void *thread_function1(void *arg) {
printf("1st: broad cast myondn");
pthread_cond_broadcast(&mycond);
return NULL;
}
void *thread_function2(void *arg) {
sleep(2);
printf("2st: broad cast myondn");
pthread_cond_broadcast(&mycond);
return NULL;
}
int main(int argc, char **argv) {
int ret;
pthread_t mythread1;
pthread_t mythread2;
pthread_mutex_init(&mymutex, NULL);
pthread_cond_init(&mycond, NULL);
if (pthread_create(&mythread1, NULL, thread_function1, NULL)) {
printf("create thread1 errorn");
}
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&mycond, &mymutex);
pthread_mutex_unlock(&mymutex);
printf("wait1n");
if (pthread_create(&mythread2, NULL, thread_function2, NULL)) {
printf("create thread2 errorn");
}
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&mycond, &mymutex);
pthread_mutex_unlock(&mymutex);
printf("wait2n");
return 0;
}
|
if (pthread_create(&mythread1, NULL, thread_function1, NULL)) {
printf("create thread1 errorn");
}
执行之后,thread_function1的pthread_cond_broadcast(&mycond);可能执行比main的pthread_cond_wait(&mycond, &mymutex);还要早,信号丢失了,程序当然就在第一个pthread_cond_wait那里就卡住了
信号量应该配合条件变量使用,应该改成:
#include
#include
#include
pthread_mutex_t mymutex;
pthread_cond_t mycond;
int ret=0,ret2=0;
void *thread_function1(void *arg) {
pthread_mutex_lock(&mymutex);
ret = 1;
printf("1st: broad cast myondn");
pthread_cond_broadcast(&mycond);
pthread_mutex_unlock(&mymutex);
return NULL;
}
void *thread_function2(void *arg) {
pthread_mutex_lock(&mymutex);
ret2 = 1;
sleep(2);
printf("2st: broad cast myondn");
pthread_cond_broadcast(&mycond);
pthread_mutex_unlock(&mymutex);
return NULL;
}
int main(int argc, char **argv) {
pthread_t mythread1;
pthread_t mythread2;
pthread_mutex_init(&mymutex, NULL);
pthread_cond_init(&mycond, NULL);
if (pthread_create(&mythread1, NULL, thread_function1, NULL)) {
printf("create thread1 errorn");
}
pthread_mutex_lock(&mymutex);
if (ret==0)
{
pthread_cond_wait(&mycond, &mymutex);
}
pthread_mutex_unlock(&mymutex);
printf("wait1n");
if (pthread_create(&mythread2, NULL, thread_function2, NULL)) {
printf("create thread2 errorn");
}
pthread_mutex_lock(&mymutex);
if (ret2==0)
{
pthread_cond_wait(&mycond, &mymutex);
}
pthread_mutex_unlock(&mymutex);
printf("wait2n");
return 0;
}
printf("create thread1 errorn");
}
执行之后,thread_function1的pthread_cond_broadcast(&mycond);可能执行比main的pthread_cond_wait(&mycond, &mymutex);还要早,信号丢失了,程序当然就在第一个pthread_cond_wait那里就卡住了
信号量应该配合条件变量使用,应该改成:
#include
#include
#include
pthread_mutex_t mymutex;
pthread_cond_t mycond;
int ret=0,ret2=0;
void *thread_function1(void *arg) {
pthread_mutex_lock(&mymutex);
ret = 1;
printf("1st: broad cast myondn");
pthread_cond_broadcast(&mycond);
pthread_mutex_unlock(&mymutex);
return NULL;
}
void *thread_function2(void *arg) {
pthread_mutex_lock(&mymutex);
ret2 = 1;
sleep(2);
printf("2st: broad cast myondn");
pthread_cond_broadcast(&mycond);
pthread_mutex_unlock(&mymutex);
return NULL;
}
int main(int argc, char **argv) {
pthread_t mythread1;
pthread_t mythread2;
pthread_mutex_init(&mymutex, NULL);
pthread_cond_init(&mycond, NULL);
if (pthread_create(&mythread1, NULL, thread_function1, NULL)) {
printf("create thread1 errorn");
}
pthread_mutex_lock(&mymutex);
if (ret==0)
{
pthread_cond_wait(&mycond, &mymutex);
}
pthread_mutex_unlock(&mymutex);
printf("wait1n");
if (pthread_create(&mythread2, NULL, thread_function2, NULL)) {
printf("create thread2 errorn");
}
pthread_mutex_lock(&mymutex);
if (ret2==0)
{
pthread_cond_wait(&mycond, &mymutex);
}
pthread_mutex_unlock(&mymutex);
printf("wait2n");
return 0;
}
|
不是应该晚于,而是如果你的信号先broadcast了,然后再wait,你可以wait得到信号吗?