【问题标题】:Pthread Sync - pthread_cond_waitPthread 同步 - pthread_cond_wait
【发布时间】:2012-02-07 06:55:31
【问题描述】:

在我的功能中的某一时刻,它会达到

pthread_cond_wait(&cond_state, &b_state);

当一个信号被发送并唤醒这个线程。它会在调用 Enter 之前立即尝试对互斥锁进行锁定吗?

    void Enter(int g, int timer){
     pthread_mutex_lock(&b_state);

     if (room.state == 2 || room.state == g)
     {
       pthread_mutex_unlock(&b_state);
       Leave();
     }
     else 
     {  
       pthread_cond_wait(&cond_state, &b_state);
       Enter(g, timer);         //Try to enter again
     }
    }

如果一个线程进入睡眠状态,我遇到一个问题,一旦它醒来,它会在调用 Enter 后卡在 mutex_lock。

【问题讨论】:

    标签: pthreads mutex race-condition


    【解决方案1】:

    是的,pthread_cond_wait 的手册页说:

    等待线程只有在另一个线程调用后才会解除阻塞 pthread_cond_signal(3) 或 pthread_cond_broadcast(3) 具有相同的 条件变量,和当前线程重新获得锁定 互斥体

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多