为什么pthread_cond_signal不起作用?

bod*_*ser 0 c multithreading pthreads

我目前正在学习所有POSIX线程(pthread).

我现在已经创建了一个简单的程序,它将共享值增加7直到10000以上然后它应该向下一个线程发出一个条件,它将它减少3直到1000以下.最后它应该将结果除以2并且main应该输出结果.

我的代码:

pthread_t threads[3];
pthread_cond_t cond_a, cond_b;
pthread_mutex_t mutex;

int counter;

void * worker_one();
void * worker_two();
void * worker_three();

int main(int argv, const char ** argc) {
    counter = 0;

    pthread_cond_init(&cond_a, NULL);
    pthread_cond_init(&cond_b, NULL);
    pthread_mutex_init(&mutex, NULL);

    pthread_create(&threads[0], NULL, worker_one, NULL);
    pthread_create(&threads[1], NULL, worker_two, NULL);
    pthread_create(&threads[2], NULL, worker_three, NULL);

    pthread_join(threads[0], NULL);
    pthread_join(threads[1], NULL);
    pthread_join(threads[2], NULL);

    printf("Value started at %d and ends with %d.\n", 0, counter);

    return 0;
}

void * worker_one() {
    printf("Worker one started.\n");

    pthread_mutex_lock(&mutex);

    printf("Worker one starting work.\n");
    while (counter < 10000) {
        counter += 7;
    }

    pthread_cond_signal(&cond_a);

    printf("Worker one finished work with: %d.\n", counter);

    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

void * worker_two() {
    printf("Worker two started.\n");

    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond_a, &mutex);

    printf("Worker two starting work.\n");
    while (counter > 1000)
        counter -= 3;

    printf("Worker two finished work with: %d.\n", counter);

    pthread_cond_signal(&cond_b);
    pthread_mutex_unlock(&mutex);

    sleep(1);

    pthread_exit(NULL);
}

void * worker_three() {
    printf("Worker three started.\n");

    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond_b, &mutex);

    printf("Worker three starting work.\n");

    counter /= 2;

    printf("Worker three finished work with: %d.\n", counter);

    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,整个执行都围绕着第一个线程.信号也被触发但是线程2没有反应.

有人能告诉我我做错了什么吗?

Ben*_*Ben 6

我在这里回答了类似的问题:Linux上的pthread条件变量,奇怪的行为.

问题是你在等待测试你想要等待的条件之前就等了.发生的是线程1在线程2等待之前发出信号,因此信号丢失,线程2将永远等待.

为了避免这种情况,首先测试你想要等待的东西,然后等待它不在这里.

编辑:好的,这是一个可能的解决方案,只有一个互斥和一个condtion(未经测试)

线程1:

pthread_mutex_lock(&mutex); 
while(thread_1_should_work == false){ // wait until the condition is satisfied
  pthread_cond_wait(&cond, &mutex); 
}

//at this point, we owe the mutex and we know thread_1_should_work is true; 

// do work 

thread_1_shoudl_work = false; 
thread_2_should_work = true; 

pthread_cond_broadcast(&cond); //wake up any waiting thread (if it's not their turn, they'll call wait again)
pthread_mutex_unlock(&mutex); 
Run Code Online (Sandbox Code Playgroud)

... 等等.