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

原文地址:https://www.cnblogs.com/eustoma/p/2418473.html