多线程虚假唤醒 Spurious wakeup

问题描述:
线程在等待signal信号时,即使等待的条件变量并没有变化,线程仍然可能被唤醒。
解决的办法是设置一个while循环,检测条件变量是否真正改变了,如果没有就继续wait

维基百科里的描述:
Spurious wakeup describes a complication in the use of condition variables as provided by certain multithreading APIs such as POSIX Threads and the Windows API.

Even after a condition variable appears to have been signaled from a waiting thread's point of view, the condition that was awaited may still be false. One of the reasons for this is a spurious wakeup; that is, a thread might be awoken from its waiting state even though no thread signaled the condition variable. For correctness it is necessary, then, to verify that the condition is indeed true after the thread has finished waiting. Because spurious wakeup can happen repeatedly, this is achieved by waiting inside a loop that terminates when the condition is true

程序示例:

static pthread_mutex_t  my_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t   my_cond;
int my_flag = 0;
int rc = 0;

线程1:
rc = pthread_cond_init(&my_cond, (pthread_condattr_t*)NULL);
if (rc != 0)
{
    return -1;
}

pthread_mutex_lock(&my_mutex);
while(my_flag == 0)
    pthread_cond_wait(&my_cond, &my_mutex);
pthread_mutex_unlock(&my_mutex);

线程2:
rc = pthread_mutex_lock(&my_mutex);
if (rc != 0)
{
    pthread_exit(NULL);
}

my_flag = 1;

rc = pthread_cond_signal(&my_cond);
if (rc != 0)
{
    pthread_exit(NULL);
}

rc = pthread_mutex_unlock(&my_mutex);
if (rc != 0)
{
    pthread_exit(NULL);
}

 

你可能感兴趣的:(Linux,多线程)