它是保证调用pthread

编程入门 行业动态 更新时间:2024-10-17 11:31:30
本文介绍了它是保证调用pthread_cond_signal将唤醒一个等待线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个普遍的问题。例如目前两个子线程都叫做调用pthread_cond_wait(安培; COND1,&安培;互斥),他们都在等待。然后,父线程调用

This is a general question. For example currently two child threads have called pthread_cond_wait(&cond1,&mutex), and they are both waiting. Then, the parent thread calls

pthread_cond_signal(&cond1); pthread_cond_signal(&cond1);

接下来,我的问题是,它是保证,无论这两个等待线程将得到唤醒?(后来假设第一个线程被唤醒释放互斥锁在执行一定阶段,使得第二线程才能获取它)。

Next, my question is, is it guaranteed that both of the two waiting threads will get woken up?(Suppose the first thread woken up releases mutex later at certain stage of execution so that the second thread can acquire it).

原因我问这个问题是因为,对于UNIX系统电平信号,该信号(SIGCHLD说)不排队,以便如果他们连续交付可能会丢失多个相同类型的信号。所以我想是调用pthread_cond_signal实现方式不同,因此如果调度发生两次让父线程的信号在一排,他们会不会迷路?

The reason for me to ask this question is because, for the unix system level signal, the signal (say SIGCHLD) is not queued so that multiple signal of same type may be lost if they are delivered consecutively. So I wonder is pthread_cond_signal implemented differently so that they will not get lost if the scheduler happen to let the parent thread signal twice in a row?

推荐答案

简单的回答:

调用pthread_cond_signal()将唤醒阻塞在条件变量的线程的至少一个 - 但更重要的是不能保证(仅供参考,使用调用pthread_cond_broadcast()来唤醒所有阻塞的线程)。

pthread_cond_signal() will wake up at least one of the threads that is blocked on the condition variable - but more than that is not guaranteed (for reference, use pthread_cond_broadcast() to wake up all blocked threads).

这里:

的调用pthread_cond_signal()调用放开线程中的至少一  被阻止在指定条件变量COND(如果有的  线程被阻塞在COND)。

The pthread_cond_signal() call unblocks at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).

在调用pthread_cond_broadcast()调用解封当前所有线程  挡在了指定的条件变量COND。

The pthread_cond_broadcast() call unblocks all threads currently blocked on the specified condition variable cond.

较长的答案:

因此​​,根据该规范,我想presume解封同步发生的,也就是说,已解除封锁的第一次调用调用pthread_cond_signal()线程将被视为畅通的第二次调用调用pthread_cond_signal(),因此,其他线程将被唤醒。

So, according to the specification, I'd presume the unblocking to happen synchronously, that is, a thread that has been unblocked by the first call to pthread_cond_signal() will be seen as unblocked by the second call to pthread_cond_signal(), and thus the other thread will be waken up.

不过,我不知道这是否是你的具体的pthread实现与否的情况下(和glibc的网站​​是pretty此刻狡猾的,所以无法获得访问code看)。

However, I do not know whether this is the case for your specific pthread implementation or not (and the glibc website is pretty dodgy at the moment, so can't get access to code to look at).

的可能,还未实现,但是,它 - 是 - 在该规格的回答:

应该指出虽然,该规范最近就得到了稍微改写如何使用调用pthread_cond_signal()和调用pthread_cond_broadcast()确定这实际上阻止在给定的条件变量的线程,但我$,并非所有的实现都赶上尚未p $ psume。

It should be noted though, that the specification recently got slightly reworded regarding how the pthread_cond_signal() and pthread_cond_broadcast() determine which threads are actually blocked on a given condition variable, but I presume that not all implementations have caught up yet.

关于这个问题进行了长时间讨论,可以这里发现,随着新的规范是

A long discussion on the subject can be found here, with the new specification being:

在调用pthread_cond_broadcast(),并调用pthread_cond_signal()函数  应原子确定哪些线程,如果有的话,被阻止  在指定的条件变量COND。这个决定  应在一个不确定的时间发生  调用pthread_cond_broadcast()或调用pthread_cond_signal()调用。  然后调用pthread_cond_broadcast()函数将取消禁止所有的  这些线程。该调用pthread_cond_signal()函数应在解除  这些线程至少一种

The pthread_cond_broadcast() and pthread_cond_signal() functions shall atomically determine which threads, if any, are blocked on the specified condition variable cond. This determination shall occur at an unspecified time during the pthread_cond_broadcast() or pthread_cond_signal() call. The pthread_cond_broadcast() function shall then unblock all of these threads. The pthread_cond_signal() function shall unblock at least one of these threads.

因此​​,得出的结论:而不规范的专家间preTER,我会说,新案文支持此同步发生的假设 - 让两个连续的通话调用pthread_cond_signal()有两个可用的阻塞的线程,将唤醒两个线程。

So, the conclusion: Without being an expert interpreter of specifications, I'd say that the new text supports the assumption of this happening synchronously - so that two consecutive calls to pthread_cond_signal() with two blocked threads available, will wake up both threads.

我不是这个100%肯定的,所以如果有人能详细,你可以这样做。

I'm not 100% sure on this though, so if anyone can elaborate, feel free to do so.

更多推荐

它是保证调用pthread

本文发布于:2023-10-09 05:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:它是   pthread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!