我现在很困惑为什么下面的代码不会打印以下内容:

My value is 0
My value is 1
My value is 2

每次运行时,我要么得到1-2个打印行,要么什么都没有,程序就一直运行到我ctrl-c为止。我觉得这可能与我使用相同的条件变量和带有3个不同线程的互斥量有关,这是否正确?任何解释都非常感谢。
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>

struct id_holder
{
 int id;
};

pthread_mutex_t intersectionMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t directionCondition = PTHREAD_COND_INITIALIZER;
struct id_holder * holder;

void * logic(void* val)
{
  struct id_holder * id_struct = (struct id_holder *) val;

  pthread_cond_wait(&directionCondition, &intersectionMutex);
  printf("My value is %d\n", id_struct->id);
  free(id_struct);
  return NULL;
}

int main(void)
{
     pthread_t threads[3];

     for(int i = 0; i <3; i++)
     {
       holder = (struct id_holder *) malloc(sizeof(struct id_holder));
       holder->id = i;
       pthread_create(&threads[i], NULL, logic, holder);
     }

     for(int i = 0; i < 3; i++)
     {
       sleep(1);
       pthread_cond_signal(&directionCondition);
     }

     for(int i = 0; i < 3; i++)
     {
       pthread_join(threads[i], NULL);
     }

     return 0;
}

最佳答案

当条件等待或发出信号时,必须在锁下完成,否则行为将无法预测,因为它可能进入竞赛条件。因此,您的代码应该如下所示:

pthread_mutex_lock(&intersectionMutex);
pthread_cond_wait(&directionCondition, &intersectionMutex);
pthread_mutex_unlock(&intersectionMutex);

main也一样(如果愿意,可以将锁移到循环之外):
for(int i = 0; i < 3; i++) {
    sleep(1);
    pthread_mutex_lock(&intersectionMutex);
    pthread_cond_signal(&directionCondition);
    pthread_mutex_unlock(&intersectionMutex);
}

但代码仍然不是100%安全的,因为主线程可以在子线程调用wait之前发出条件信号。尽管由于主函数中的sleep()在这里是非常不可能的,但是通常应该有一个变量来标识是否实际需要等待。换句话说,条件不是队列,但可以用于创建队列。

关于c - 了解pthread,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40298566/

10-12 05:53