问题描述
在与一些C code接口部分QT GUI线程(一个pthread的线程)的背景下,我偶然发现了以下的问题:我推出QT GUI线程和,我的C线恢复其路径之前,我需要以确保所有的QT GUI线程里面的图形对象已经建成,他们是有效的QObject(因为C code将调用的QObject:连接()
上这些信息)。
In the context of interfacing some QT GUI thread (a pthread thread) with some C code, I stumbled over the following problem: I launch the QT Gui thread and, before my C thread resuming its path, I need to make sure that all the graphical objects inside the QT Gui thread had been constructed and they are valid QObjects (since the C code will call QObject:connect()
on those).
引言让一旁,等待通过调用pthread_cond_wait取得()
+条件变量+在C线程关联互斥:
Introduction let aside, the waiting is made through a pthread_cond_wait()
+ a condition variable + an associated mutex in the C thread:
int isReady=0;
pthread_mutex_lock(&conditionReady_mutex);
while(isReady==0) {
pthread_cond_wait(&conditionReady_cv, &conditionReady_mutex);
}
pthread_mutex_unlock(&conditionReady_mutex);
在另一方面,所述QT桂螺纹构造其图形对象,然后用信号它
On the other hand, the QT Gui thread constructs its graphical objects and then signals it with:
pthread_mutex_lock(&conditionReady_mutex);
isReady=1;
pthread_cond_broadcast(&conditionReady_cv);
pthread_mutex_unlock(&conditionReady_mutex);
基本的东西,如你所见。但问题是:在Qt的GUI线程,我一直在使用调用pthread_cond_broadcast()
,以确保我的C线程被唤醒,是肯定的。是的,在我目前的应用程序中,我只有一个C线程和Qt的GUI线程,和调用pthread_cond_signal()
应该做醒来的C线(因为它是工作保证唤醒至少一个线程,以及C线是唯一的一个)。
Basic stuff, as you see. But the question is: in the Qt Gui thread, I've been using the pthread_cond_broadcast()
, in order to make sure that my C thread is woken up, for sure. Yes, in my current application, I only have a C thread and a Qt Gui thread, and pthread_cond_signal()
should do the job of waking up the C thread (since it is guaranteed to wake up at least one thread, and the C thread is the only one).
不过,在更一般的情况下,让我们说我有三C线程,但我想他们中的一个(或两个)被唤醒。 A(二)特定线程(S)。我如何保证?
But, in a more general context, let's say I have three C threads, but I want one (or two) of them to be woken up. A (two) specific thread(s). How do I ensure that?
如果我用调用pthread_cond_signal()
,也可以简单地醒来只有第三个线程,这将完全错过了这一点,因为我感兴趣的一个线程是不醒了。 OTOH,唤醒所有线程,甚至那些不需要通过调用pthread_cond_broadcast()
,这将是矫枉过正。
If I use pthread_cond_signal()
, that could simply wake up only the third thread, which would completely miss the point, since the one thread of interest for me is not woken up. OTOH, waking up all the threads, even those which are unneeded, through pthread_cond_broadcast()
, that would be overkill.
有一种方法来告诉调用pthread_cond_signal()
来唤醒哪个线程?
There is a way to tell pthread_cond_signal()
which thread to wake up?
或者,我应该以有超过被唤醒了调用pthread_cond_broadcast线程组更细粒度引入更多的条件变量()
?
Or, should I introduce more condition variables in order to have a finer granularity over the groups of threads that are woken up with pthread_cond_broadcast()
?
感谢您。
推荐答案
是的,如果你需要一个特定的线程被唤醒,那么你要么需要广播唤醒,或使用一个单独的条件变量的线程。
Yes, if you require a specific thread to be woken up then you either need to broadcast the wakeup, or use a separate condition variable for that thread.
这篇关于我是被迫以通过使用pthread_cond_broadcast(超过pthread_cond_signal会),以保证*我*线程被唤醒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!