互斥对象是线程程序必需的工具,但它们并非万能的。
Mutexes are necessary tools for threaded programs, but they can't do everything.
第一个线程锁住互斥并把数据添加到队列中,而其他线程等待轮到它们操作。
The first thread locks the mutex and appends data to the queue, while the other threads wait for their turn.
如果进行调用的线程并不拥有那个互斥对象,则这个调用可能会失败。
This call might fail if the calling thread does not own the mutex object.
现在互斥对象已被解锁,其它线程可以访问和修改已链接列表,可能还会添加项。
Now that the mutex is unlocked, other threads can access and modify the linked list, possibly adding items.
创建和使用互斥量的过程比仅仅是开始一个线程的过程要稍微复杂一些。
The procedure for creating and using a mutex is a bit more complicated than the procedure for starting a thread.
POSIX提供了互斥函数来创建临界区,用于实施单线程对对象(一块内存)的独占访问。
POSIX provides the mutex function to create critical sections that enforce exclusive access to an object (a piece of memory) by a single thread.
如果在读线程能够获得互斥锁之前发生了超时,那么不需要进行处理。
If the timeout has occurred before the reader thread could acquire the mutex, then no processing need be done.
如果调用线程并不拥有这个互斥信号量,那么这个函数的执行将会失败。
If the calling thread has no ownership of this mutex, this function fails.
在不只一个线程访问一个互斥的变量时,所有线程都必须使用同步,否则就可能会发生一些非常糟糕的事情。
When more than one thread accesses a mutable variable, all threads must use synchronization, or else some very bad things can happen.
可以推测到,当线程试图锁定一个未加锁的互斥对象时,POSI x线程库将同意锁定,而不会使线程进入睡眠状态。
As you may have guessed, the POSIX threads library will grant a lock without having put the thread to sleep at all if a thread tries to lock an unlocked mutex.
当线程正在做其它事情的时候(由于互斥对象当前是锁定的),如果希望锁定互斥对象,这个调用就相当方便。
This call is handy when you want to lock a mutex while your thread is doing something else (because the mutex is currently locked).
如果另一个线程锁定了那个互斥,则pthread _ mutex _ trylock将不会阻塞。
If another thread locks the mutex, the pthread_mutex_trylock will not block.
它还允许您原子地(atomically)解除互斥的锁定,并等待条件变量,而不会有干涉其他线程的可能。
It also allows you to unlock the mutex and wait on the condition variable atomically, without the possible intervention of another thread.
当与互斥一起使用时,pthreads条件变量可以在线程之间提供基于事件的同步机制,不过这是同步的。
When used along with the Mutex, pthreads conditional variables provide event-based synchronization between threads, but they are synchronous.
之后它创建一个新的线程同时增量threadcount;在完成之后,它解锁互斥量。
After locking that, it creates a new thread and increments threadcount; after it's done, it unlocks the mutex.
锁定互斥对象时,线程将调用 pthread_cond_wait(&mycond,&mymutex)。
While still holding the mutex lock, our thread will call pthread_cond_wait(&mycond,&mymutex).
一旦 pthread_cond_wait()锁定了互斥对象,那么它将返回并允许1 号线程继续执行。
Once pthread_cond_wait() has the lock, it will then return and allow thread 1 to continue execution.
现在,有两个线程需要使用这两个互斥量。
Now, imagine that two threads want to use them. Thread one does this.
第一个读线程锁住互斥锁,发现队列是空的,然后在 _cond 上阻塞自身,这会隐式地释放互斥锁。
The first reader thread locked the mutex, realized that the queue is empty, and blocked itself on _cond, which implicitly released the mutex.
这么做会唤醒所有等待条件变量_ 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.
临界段操作时坚持互斥锁(mutual exclusion)原则(当一个线程处于临界段中时,其他所有线程都不能进入临界段)。
Critical sections operate on the principle of mutual exclusion (when a thread is within a critical section, all other threads are excluded from entering).
如果能够做到这一点,线程代码将是非常高效的,并且不会占用宝贵的互斥对象锁。
If you can do this, your threaded code will be really efficient and it won't tie up valuable mutex locks.
这两个函数调用的作用就是警告其它线程,要它们继续睡眠并等待轮到它们对互斥对象加锁。
The calls act as a warning to other threads to go to sleep and wait their turn for the mutex lock.
两个线程不能同时对同一个互斥对象加锁。
No two threads can have the same mutex locked at the same time.
同时,还要快速将互斥对象解锁,以便其它线程能够进行任何必需的更改。
At the same time it will also quickly unlock the mutex so that others can make any necessary changes.
这对于等待同一互斥锁的其他线程有不利影响,因为等待时间现在会更长。
For other threads waiting on the same mutex, this has a negative effect, because the wait time is now even longer.
操作系统调度程序决定哪个线程获得对互斥锁的控制权—通常,等待时间最长的读线程先读取数据。
The operating system scheduler determines which thread gets control of the mutex next-typically, the reader thread that has waited the longest gets to read the data first.
因为添加和取出数据操作使用相同的互斥锁,所以读取数据的速度会影响写数据的线程访问锁。
Because you're sharing the same mutex for push and pop operations, the data-read speed is somewhat compromised as writer threads access the lock.
如果到超时时间段结束时还没有被唤醒,读线程需要唤醒自身并释放互斥锁。
If not awake otherwise, at the end of the timeout, the reader needs to wake itself up and release the mutex.
可能因为分配的时间片结束,持有互斥锁的线程被取消调度。
A thread holding a mutex can be de-scheduled, perhaps because it was the end of its time-slice.
应用推荐