简体   繁体   English

C编程中的并发线程

[英]concurrent threads in C programming

I have encountered a problem while implementing wait and signal conditions on multiple threads. 在多个线程上实现等待和信号条件时遇到了问题。

A thread needs to lock a mutex and wait on a condition variable until some other thread signals it. 线程需要锁定互斥锁并等待条件变量,直到其他线程发出信号。 In the meanwhile, another thread locks the same mutex and waits on the same condition variable. 同时,另一个线程锁定相同的互斥锁并等待相同的条件变量。 Now, the thread which is running concurrently throughout the process signals the condition variable but I want only the first thread that is waiting must be signalled and not the others. 现在,在整个过程中并发运行的线程会向条件变量发出信号,但我只希望必须发出正在等待的第一个线程的信号,而其他线程则没有。

If two threads wait on the same condition variable, they must be prepared to handle the same conditions, or you must carefully construct your program so they are never waiting on the condition variable at the same time. 如果两个线程在同一个条件变量上等待,则它们必须准备处理相同的条件,或者您必须仔细构造程序,以使它们永远不会在同一时间在条件变量上等待。

Why does this notification have to be handled by the first thread and not the second? 为什么此通知必须由第一个线程而不是第二个线程处理?

You may be better off with two separate condition variables. 使用两个独立的条件变量可能会更好。

Use pthread_cond_signal() to wake up one of the threads. 使用pthread_cond_signal()唤醒其中一个线程。

However, more than one might be awoken; 但是,可能不止一个被唤醒。 this is termed spurious wakeup . 这被称为伪唤醒 You need a variable to track your application state, as described in the manual page linked above. 您需要一个变量来跟踪您的应用程序状态,如上面链接的手册页所述。

Your requirement is impossible. 您的要求是不可能的。 You say "... I want only the first thread that is waiting must be signalled and not the others." 您说“ ...我只希望发出正在等待的第一个线程,而不发出其他信号。” But condition variables never, ever provide any way to ensure a thread isn't signaled. 但是条件变量永远不会提供任何方式来确保不发出线程信号。 So if you have a requirement that a thread must not be signaled, you cannot use condition variables. 因此,如果您必须禁止发出线程信号,则不能使用条件变量。

You must always use a condition variable like this: 您必须始终使用这样的条件变量:

while(NotSupposedToRun)
  pthread_cond_wait(...);

So if the thread wakes up when it's not supposed to, the while is still false and the thread just goes back to sleep. 因此,如果线程在不应该被唤醒时唤醒,则while仍然为false ,并且线程仅返回睡眠状态。 This is mandatory because POSIX does not ever provide any guarantee that a thread won't be woken. 这是强制性的,因为POSIX永远无法保证不会唤醒线程。 An implementation is perfectly free to implement pthread_cond_signal as a call to pthread_cond_broadcast and unblock all threads on every signal if it wants to. 实现是完全自由地实现pthread_cond_signal作为调用pthread_cond_broadcast和解锁每个信号的所有线程,如果它想。

Because condition variables are stateless, the implementation never knows whether a thread is supposed to be woken or not for sure. 由于条件变量是无状态的,因此实现永远无法确定是否应该唤醒线程。 It is your job to call pthread_cond_wait always, and only, when a thread should not be running. 仅在不应该运行线程的情况下,始终调用pthread_cond_wait是您的工作。

See http://en.wikipedia.org/wiki/Spurious_wakeup for more details. 有关更多详细信息,请参见http://en.wikipedia.org/wiki/Spurious_wakeup

If you cannot precisely specify the wakeup conditions for each thread in a while loop like the one above, you should not be using condition variables. 如果您不能像上述那样精确地在while循环中为每个线程指定唤醒条件, while不应使用条件变量。

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

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