不过,每个条件变量都应该伴有一个互斥。
这正是POSIX条件变量能做的事!
That's precisely what POSIX condition variables can do for you!
还要注意,两个条件变量使用相同的互斥锁。
条件变量允许线程按照共享资源的一个值同步。
Condition variables allow threads to synchronize to a value of a Shared resource.
使用TABLE命令也可以操作模型条件变量。
Variations in model conditions can also be made using the TABLE command.
因此,最后两个读线程都等待条件变量,互斥锁没有被锁住。
Therefore, at the end of it all, you now have two reader threads, both waiting on the condition variable, and the mutex is unlocked.
push方法不依赖于任何条件变量,所以没有额外的等待。
Now, the push method doesn't depend on any condition variables, so no extra waiting there.
一旦计数器达到界限就激活的线程会等待条件变量。
The thread that activates once the counter reaches the limit would wait on the condition variable.
线程模块提供了许多同步原语,包括信号量、条件变量、事件和锁。
The threading module does provide many synchronization primatives, including semaphores, condition variables, events, and locks.
一个条件变量允许任务等待一个同步原语,以进入临界区。
A condition variable allows a task to wait on a synchronization primitive within a critical region.
更新:有条件变量可以控制在多线程环境中任务的执行顺序。
UPD: with condition variables you can control the order of tasks execution within multithreading environment.
条件变量让开发者能够实现一个条件,在这个条件下线程执行然后被阻塞。
A condition variable enables developers to implement a condition in which a thread executes and then blocked.
当队列是空的时候,读线程现在并不抛出异常,而是在条件变量上阻塞自身。
Instead of throwing an exception when the queue is empty, the reader thread now blocks itself on the condition variable.
互斥锁有个我没讲到的方面,很多操作系统支持条件变量的概念。
An aspect of the mutex I haven't covered here is that many operating systems support the concept of a condition variable.
在有些实现中,线程可能偶尔在没有信号送到条件变量的情况下醒过来。
In some implementations, threads may occasionally wake up without a signal sent to their condition variable.
这么做会唤醒所有等待条件变量_ cond的读线程;读线程现在隐式地争夺互斥锁。
Doing so awakens all the reader threads that were waiting on the condition variable _cond; the reader threads now implicitly compete for the mutex lock as and when it is released.
对于清单13和清单14,要注意的第一点是,这个阻塞队列有两个条件变量而不是一个。
The first thing of note in Listing 13 and Listing 14 is that there are two condition variables instead of the one that the blocking queue had.
接下来,读线程需要确保(这是第二个检查)它等待条件变量的时间不超过指定的超时时间。
Next, the reader thread needs to ensure (and this is the second check you perform) that it does not wait on the condition variable any more than the specified timeout period.
推荐的方法是使用条件变量—即pthread_cond _ t类型的变量。
The recommended method is to use condition variables-that is, variables of type pthread_cond_t.
为解决此缺憾,我使用POSIX条件变量模拟同步原语,并在一系列文章中对此进行了概述。
To work around this omission, I use the POSIX condition variable emulations synchronization primitives, which are outlined in the series of articles.
而POSIX条件变量将是我下一篇文章的主题,其中将说明如何正确使用条件变量。
And POSIX condition variables are the subject of my next article, where I'll show you exactly how to use them.
在Linuxpthreads中,使用pthread_cond_destroy来销毁条件变量。
In Linux pthreads, the pthread_cond_destroy is used to destroy the conditional variable.
如果队列满了,写线程等待_ wcond条件变量;读线程在从队列中取出数据之后需要通知所有线程。
If the queue is full, the writer thread waits on the _wcond condition variable; the reader thread will need a notification to all threads after consuming data from the queue.
为了达到那个目的,线程模块提供了许多同步化的原生支持,包括:锁,事件,条件变量和信号灯。
To that end, the threading module provides a number of synchronization primitives including locks, events, condition variables, and semaphores.
当与互斥一起使用时,pthreads条件变量可以在线程之间提供基于事件的同步机制,不过这是同步的。
When used along with the Mutex, pthreads conditional variables provide event-based synchronization between threads, but they are synchronous.
当不再使用条件变量时,应该调用pthread_cond_destroy释放它在初始化时分配的资源。
When you're done with a condition variable, you can release any resources allocated during initialization with a call to pthread_cond_destroy .
它还允许您原子地(atomically)解除互斥的锁定,并等待条件变量,而不会有干涉其他线程的可能。
It also allows you to unlock the mutex and wait on the condition variable atomically, without the possible intervention of another thread.
任务设计变量包括任务输入变量、任务条件变量和任务结果变量,每个变量又包含一些任务设计因素。
Task design variables include task input, task condition and task outcome varia-bles, and each variable still includes some design factors.
如果另一个线程对一个条件变量调用pthread_cond_signal,那么那个等待这个条件而被阻塞的线程就会被唤醒。
If another thread calls pthread_cond_signal on a condition, then a thread that was waiting on that condition variable is woken up.
这个操作要消耗一定的时间,实际上,这个时间太长了,可能在进行这项操作的同时,线程所等待的条件变量的值在这期间已经改变了。
This may take some time. In fact, it may take so much time that the condition you were waiting for is no longer the case.
应用推荐