也就是说,如果signal的时候没有线程在condition wait,那么本次signal就没有效果,后续的线程进入condition wait之后,无法被之前的signal唤醒。
测试代码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *test_thread(void *arg)
{
printf("Signal main thread...\n");
pthread_cond_signal(&cond);
return NULL;
}
int main()
{
pthread_t thread;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread, NULL, test_thread, NULL);
sleep(5);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("Main thread signaled, quit...\n");
return 0;
}
#include <pthread.h>
#include <unistd.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *test_thread(void *arg)
{
printf("Signal main thread...\n");
pthread_cond_signal(&cond);
return NULL;
}
int main()
{
pthread_t thread;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread, NULL, test_thread, NULL);
sleep(5);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("Main thread signaled, quit...\n");
return 0;
}