pthread_cond_signal只能唤醒已经处于pthread_cond_wait的线程

    也就是说,如果signal的时候没有线程在condition wait,那么本次signal就没有效果,后续的线程进入condition wait之后,无法被之前的signal唤醒。
    测试代码:   

 1 #include <stdio.h>
2 #include <pthread.h>
3 #include <unistd.h>
4
5 pthread_cond_t cond;
6 pthread_mutex_t mutex;
7
8 void *test_thread(void *arg)
9 {
10 printf("Signal main thread...\n");
11 pthread_cond_signal(&cond);
12 return NULL;
13 }
14
15 int main()
16 {
17 pthread_t thread;
18
19 pthread_cond_init(&cond, NULL);
20 pthread_mutex_init(&mutex, NULL);
21
22 pthread_create(&thread, NULL, test_thread, NULL);
23
24 sleep(5);
25 pthread_mutex_lock(&mutex);
26 pthread_cond_wait(&cond, &mutex);
27
28 printf("Main thread signaled, quit...\n");
29 return 0;
30 }

http://www.cnblogs.com/super119/archive/2011/07/29/2120761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值