据说Mustex需要保护有条件的变迁者。
Is the reference here to the actual condition variable declared as pthread_cond_t
OR
A normal shared variable count
whose values decide the signaling and wait.
?
据说Mustex需要保护有条件的变迁者。
Is the reference here to the actual condition variable declared as pthread_cond_t
OR
A normal shared variable count
whose values decide the signaling and wait.
?
这里指的是实际状况变数,即被宣布为“read”-cond_t或正常的共有变量,其价值决定信号和等待?
两者都提及。
缩略语这样说,就可以核对共同变量(count
,在您的提问中),如果该变数的价值符合预期条件,则在<代码>pthread_cond_wait(>内进行的等待将原样进行。
待解决的问题是,你有两个单独的行动需要原样:
count
pthread_cond_wait()
if the condition isn t met yet.如果在<代码>pthread_cond_signal(>上没有校对,那么信号就没有。 因此,如果存在使上文所列两项行动相互相对而言的顺差,你可以发现下列情况:
count
is non-zero阅读 B将在其加入时发出信号count
。 (将建立<条码>除零外的条目/编码>)
count
and finds that it s zeropthread_cond_wait()
, thread "B" comes along and increments count
to 1 and calls pthread_cond_signal()
. That call actually does nothing of consequence since "A" isn t waiting on the pthread_cond_t
object yet.pthread_cond_wait()
, but since condition variable signals aren t remembered, it will block at this point and wait for the signal that has already come and gone.自动取款机(只要所有透镜都遵循规则)使得项目2无法在项目1和3之间发生。 读到“B”的唯一途径是,在“A”栏目中,或者在“A”之后,该信号已经等待。
条件变数必须始终与相互交织在一起,以避免种族状况,如果一线read子准备在条件变数上等待,而另一条深线则表明在一线read子实际等待之前的状况。
http://sourceware.org/pthreads-win32/manual/pthread_cond_init.html”
Some Sample:
图1
pthread_mutex_lock(cond_mutex);
while(i<5)
{
pthread_cond_wait(cond, cond_mutex);
}
pthread_mutex_unlock(cond_mutex);
图2
pthread_mutex_lock(cond_mutex);
i++;
if(i>=5)
{
pthread_cond_signal(cond);
}
pthread_mutex_unlock(cond_mutex);
正如你在上文中可以看到的那样,变体保护的是造成这一状况的变数。 当我们发现这一条件没有得到满足时,我们就陷入了一种条件,即暗中释放了mut子,从而使信号的read得以获取,并在一片和避免种族状况上开展工作。
Now, as per your question, if the signalling thread signals first, it should have acquired the mutex before doing so, else the first thread might simply check the condition and see that it is not being met and might go for condition wait and since the second thread has already signalled it, no one will signal it there after and the first thread will keep waiting forever.So, in this sense, the mutex is for both the condition & the conditional variable.
每一版面都说明,通航未分离的原因是,通过合并,业绩有显著改善,他们预计,由于共同的种族条件,如果你不使用通航道,几乎总是会做任何事。
https://linux.die.net/man/3/pthread_cond_wait/a。
Features of Mutexes and Condition Variables
It had been suggested that the mutex acquisition and release be decoupled from condition wait. This was rejected because it is the combined nature of the operation that, in fact, facilitates realtime implementations. Those implementations can atomically move a high-priority thread between the condition variable and the mutex in a manner that is transparent to the caller. This can prevent extra context switches and provide more deterministic acquisition of a mutex when the waiting thread is signaled. Thus, fairness and priority issues can be dealt with directly by the scheduling discipline. Furthermore, the current condition wait operation matches existing practice.
我认为,更好的使用案例可能有助于更好地解释有条件的变量及其相关的变量。
我使用权势变量实施以下简称:。 基本上,在我有15个(数据机)的镜子里,我使用这个数据。 我祝愿他们等到所有数据计划完成初步编制之前。 一旦他们完成(内部)数据初步化,他们就可以开始处理数据。
这里是法典。 通知一复制了从波斯特的算法,因为我在此特殊应用中使用模板:
void LinuxPlatformManager::barrierSync()
{
// Algorithm taken from boost::barrier
// In the class constructor, the variables are initialized as follows:
// barrierGeneration_ = 0;
// barrierCounter_ = numCores_; // numCores_ is 15
// barrierThreshold_ = numCores_;
// Locking the mutex here synchronizes all condVar logic manipulation
// from this point until the point where either pthread_cond_wait() or
// pthread_cond_broadcast() is called below
pthread_mutex_lock(&barrierMutex_);
int gen = barrierGeneration_;
if(--barrierCounter_ == 0)
{
// The last thread to call barrierSync() enters here,
// meaning they have all called barrierSync()
barrierGeneration_++;
barrierCounter_ = barrierThreshold_;
// broadcast is the same as signal, but it signals ALL waiting threads
pthread_cond_broadcast(&barrierCond_);
}
while(gen == barrierGeneration_)
{
// All but the last thread to call this method enter here
// This call is blocking, not on the mutex, but on the condVar
// this call actually releases the mutex
pthread_cond_wait(&barrierCond_, &barrierMutex_);
}
pthread_mutex_unlock(&barrierMutex_);
}
通知:进入barrierSync(
)的每一条透镜都锁定了自动取款机,使自动取款机锁与拨号为pthread_cond_wait(
或pthread_mutex_unlock(>
原子。 并请注意,如上所述。 在这一点上,它还提到,如果你打电话pthread_cond_wait()
,而没有首先锁定该表。
If pthread_cond_wait()
did not release the mutex lock, then all threads would block on the call to pthread_mutex_lock()
at the beginning of the barrierSync()
method, and it wouldnt be possible to decrease the barrierCounter_
variables (nor manipulate related vars) atomically (nor in a thread safe manner) to know how many threads have called barrierSync()
因此,为了总结所有这一切,与有条件可变性相关的变量是:not,用于保护有条件可变性本身,但用于确定与条件(barrierCounter_
等)相关的逻辑。 当等待条件成熟时,他们实际上会阻挡在条件性可变性上,>not上。 电话pthread_cond_broadcast/signal(
)将予以打开。
Here is another resource related to pthread_cond_broadcast()
and pthread_cond_signal()
for an additional reference.
How do you run several pthreads, in C, and detect the first to terminate? I m thinking there has got to be an interface similar to select() for sockets to do this with threads. Thanks, Chenz
How do I check if a thread is terminated? In my case, I have my_pthread[5] and I want to check if any of 5 threads has finished its job (is terminated? - I m not sure) then I can give them more work ...
What is the difference between Go s multithreading approach and other approaches, such as pthread, boost::thread or Java Threads?
Does anybody know of (or can post) an example of the use of thread-specific data? I m looking for something that s clearly explained and easy to understand. I ve got a global char * variable that I m ...
I m having trouble passing a pointer to a structure as an argument to a thread cancellation cleanup handler. Here s some sample code that blows up when it hits the compiler. Any idea what I m doing ...
i was trying to write a simple multithreaded program. It is dumping the core. I have my function that is called when a thread is created below: void *BusyWork(void *t) { int i; int *tid; int ...
On unix, how could we know whether the system is multiprocessor or uniprocessor?
My current understanding of condition variables is that all blocked (waiting) threads are inserted into a basic FIFO queue, the first item of which is awakened when signal() is called. Is there any ...