【问题标题】:C pthread_cond_wait() doesnt resume program after pthread_cond_signal()C pthread_cond_wait() 在 pthread_cond_signal() 之后不恢复程序
【发布时间】:2020-10-12 14:36:29
【问题描述】:

我想测试 pthread_cond_wait 来暂停一个线程,但它只是在 printf("thread2");声明。

我的代码:

#include <stdio.h>
#include <pthread.h>
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
volatile int done = 1;


void *testf(void* no) {
    while (1) {
        printf("thread1\n");
        pthread_mutex_lock(&mut);
        pthread_cond_wait(&cond, &mut);
        pthread_mutex_unlock(&mut);
        printf("thread2\n");
        done = 0;
    }
}




int main() {
    printf("mthread1\n");
    pthread_t pt;
    pthread_create(&pt, NULL, &testf, NULL);
    printf("mthread2\n");
    
    pthread_mutex_lock(&mut);
    printf("mthread3\n");
    pthread_cond_signal(&cond);
    printf("mthread4\n");
    pthread_mutex_unlock(&mut);
    printf("mthread5\n");

    while (done);
    return 0;
}

为什么在 cond_signal() 之后没有恢复?它应该恢复。

【问题讨论】:

  • 看起来对我来说是个僵局。
  • 主线程在其他线程调用pthread_cond_wait之前调用pthread_cond_signal。当它开始等待时,它永远不会等待,因为它永远不会收到信号,因为信号已经发生了。
  • 您必须确保在调用 pthread_cond_signal() 之前启动了辅助线程。因为当 pthread_create() 返回时,你并不确定辅助线程是否启动。
  • 条件变量的信号不排队。如果在发出信号时没有任何东西在等待,则将其忽略。
  • 屏障可以在这里起作用:pthread_barrier_init(), pthread_barrier_wait()

标签: c multithreading pthreads


【解决方案1】:

由于条件变量用于设置/检查变量,我可以提出以下增强代码来测试机制。 “完成”是要检查的变量。我在主线程中添加了一个 sleep() ,否则在大多数情况下,它有时间在辅助线程有机会检查它并至少循环一次之前设置“完成”变量。

补充说明:

与请求中提供的代码相比:即使pthread_cond_signal()的信号丢失,我们仍然无法确保在主线程中pthread_create()返回时启动辅助线程,辅助线程首先检查受互斥体保护的“完成”变量的值。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
volatile int done = 0;


void *testf(void* no) {

        printf("thread 2 looping\n");

        // Lock the mutex to check the variable
        pthread_mutex_lock(&mut);

    do {

        if (done) {
          pthread_mutex_unlock(&mut);
          printf("thread 2 exiting\n");
          break;
        }

        // The mutex is locked and will be unlocked inside
        // pthread_cond_wait()

        pthread_cond_wait(&cond, &mut);
        printf("thread 2 signaled\n");

        // The mutex is locked

    } while (1);
}




int main() {

    printf("thread1 starting...\n");
    pthread_t pt;
    pthread_create(&pt, NULL, &testf, NULL);
    printf("thread 1 created thread 2\n");

    printf("thread 1 is doing some dummy work\n");
    sleep(1);

    // Lock the mutex to check/set the variable
    pthread_mutex_lock(&mut);
    done = 1;
    printf("thread 1 signaling thread 2\n");
    pthread_cond_signal(&cond);
    printf("thread 1 releases the mutex\n");
    pthread_mutex_unlock(&mut);

    printf("thread 1 waiting end of thread 2...\n");
    pthread_join(pt, NULL);

    return 0;
}

【讨论】:

  • 好的,我在答案中添加了注释。
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
相关资源
最近更新 更多