简体   繁体   English

在线程中调用 pthread_cond_wait 是否会解锁互斥锁

[英]Does calling pthread_cond_wait in a thread unlock the mutex

Man pages say this pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be signaled .手册页说这个pthread_cond_wait 以原子方式解锁互斥锁(根据 pthread_unlock_mutex)并等待条件变量 cond 发出信号
But then thread start_function could be entered by any other thread causing two threads to be in the critical block.但是随后任何其他线程都可以进入线程start_function ,从而导致两个线程处于关键块中。

    void* start_function(){
         pthread_mutex_lock(&mutex);
         // critical block
         pthread_mutex_unlock(&mutex);
    }
    
    int main(){
// code to create threads
        pthread_cond_wait(&cond, &mutex);
    }
    

So in the above code after it hits pthread_cond_wait it will unlock the mutex?那么在上面的代码中,在它达到 pthread_cond_wait 之后它会解锁互斥锁吗?

You dont need to have a condition when you creating threads.In Mutual Exclusion only one process can run critical area at a time.A process remains in the critical area finite time and when its done its work and no one process remains in the critical area then with condition signals the other thread is ready to run also this critical area.All this work is done to avoid impasse.Also when a process stops outside of critical block it must not interacts with other processes.So your example must be:创建线程时不需要条件。在互斥中,一次只有一个进程可以运行临界区。一个进程在有限时间内保持在临界区,当它完成工作时,没有一个进程留在临界区然后有了条件信号,另一个线程也准备好运行这个关键区域。所有这些工作都是为了避免僵局。此外,当一个进程在关键块之外停止时,它不能与其他进程交互。所以你的例子必须是:

void* start_function(){
         pthread_mutex_lock(&mutex);
           pthread_cond_wait(&cond, &mutex);
         // critical block or critical area

         // you can use signals here 
         pthread_mutex_unlock(&mutex);
    }
    
    int main(){
        // code to create threads
        for(int i=0;i<10;i++){
             pthreaf_create(x[i],&start_function,...,NULL);
       }
    }

It is probably at the end of the program some pthreads have been stuck at con_wait(), so you need a broadcast() to free all threads可能是在程序结束时,一些 pthreads 卡在了 con_wait(),所以你需要一个 broadcast() 来释放所有线程

Man pages say this pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be signaled .手册页说这个pthread_cond_wait 以原子方式解锁互斥锁(根据 pthread_unlock_mutex)并等待条件变量 cond 发出信号

Yes, right after they say that that function是的,就在他们说 function

shall be called with mutex locked by the calling thread or undefined behavior results.应在调用线程锁定的互斥锁或未定义的行为结果的情况下调用。

(emphasis added). (强调补充)。 You go on to remark,您对 go 发表评论,

But then thread start_function could be entered by any other thread causing two threads to be in the critical block.但是随后任何其他线程都可以进入线程start_function ,从而导致两个线程处于关键块中。

If you invoke undefined behavior by calling pthread_cond_wait() without holding the specified mutex then anything can happen -- the behavior is undefined.如果您通过调用pthread_cond_wait()而不持有指定的互斥锁来调用未定义的行为,那么任何事情都可能发生——该行为是未定义的。 It is possible that the undefined behavior manifests simply as two threads executing in the critical region at the same time, with whatever follow-on effects that may have, but it is also possible that the program crashes, then or later, or corrupts memory, or deadlocks, or tweets all your passwords, among a universe of possibilities.未定义的行为可能只是表现为两个线程同时在临界区执行,可能会产生任何后续影响,但也有可能程序崩溃,然后或以后,或损坏 memory,或死锁,或在推特上发布您所有的密码,在众多可能性中。

So in the above code after it hits pthread_cond_wait it will unlock the mutex?那么在上面的代码中,在它达到 pthread_cond_wait 之后它会解锁互斥锁吗?

Impossible to say.没法说。 Again, the behavior is undefined.同样,行为未定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM