if possible, just design your program never to re-lock a mutex it already has.
如果可能,在设计程序时决不要锁定一个已经锁定的互斥量。
the mutex object must be declared; once declared, it must be initialized.
互斥量对象必须先被声明;声明后还必须初始化。
however, a mutex should accompany each condition variable.
不过,每个条件变量都应该伴有一个互斥。
the calls act as a warning to other threads to go to sleep and wait their turn for the mutex lock.
这两个函数调用的作用就是警告其它线程,要它们继续睡眠并等待轮到它们对互斥对象加锁。
why mutex at all?
为什么要用互斥对象?
the mutex kind can be set in two ways.
可以以两种方式设置互斥的类型。
the first thread locks the mutex and appends data to the queue, while the other threads wait for their turn.
第一个线程锁住互斥并把数据添加到队列中,而其他线程等待轮到它们操作。
if the mutex is currently unlocked you「ll get the lock, and this function will return zero.
如果互斥对象当前处于解锁状态,那么您将获得该锁并且函数将返回零。
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.
操作系统调度程序决定哪个线程获得对互斥锁的控制权 —通常,等待时间最长的读线程先读取数据。
it also checks whether the mutex is unlocked at that time (see listing 9).
它还会检查互斥在那个时刻是不是解除锁定的(见清单 9)。
this function takes the handle to the mutex object and waits until it is signaled or timeout occurs.
这个函数会使用一个互斥对象的句柄,并一直等待,直到它变为有信号状态或超时为止。
however, if the mutex is locked this call won」t block.
然而,如果互斥对象已锁定,这个调用也不会阻塞。
this function gets a pointer to the mutex it is trying to lock.
这个函数会获得一个指向它正在尝试锁定的互斥的指针。
but you can also create a mutex dynamically.
但是还可以动态地创建互斥对象。
posix provides the mutex function to create critical sections that enforce exclusive access to an object (a piece of memory) by a single thread.
posix提供了互斥函数来创建临界区,用于实施单线程对对象(一块内存)的独占访问。
this routine is called twice—before and after mutex acquisition—to determine whether further processing is required based on the time elapsed.
在获取互斥锁之前和之后各调用这个例程一次,从而根据经过的时间决定是否需要进一步处理。
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.
因为添加和取出数据操作使用相同的互斥锁,所以读取数据的速度会影响写数据的线程访问锁。
do we need to lock and unlock the mutex before and after the increment respectively?
那么,是否需要在加一操作语句前后分别锁定和解锁互斥对象呢?
this creates a new mutex and initializes the structure.
这将创建一个新的互斥锁并初始化其结构。
at the same time it will also quickly unlock the mutex so that others can make any necessary changes.
同时,还要快速将互斥对象解锁,以便其它线程能够进行任何必需的更改。
if you can do this, your threaded code will be really efficient and it won」t tie up valuable mutex locks.
如果能够做到这一点,线程代码将是非常高效的,并且不会占用宝贵的互斥对象锁。
mutexes have names, and only one mutex of a given name can exist on a machine at a time.
互斥有名称,并且,一个给定名称的互斥在同一机器上同一时刻只能存在一个。
mutex me!
互斥我吧!
if the mutex already happens to be locked, the caller will go to sleep.
如果碰巧已经锁定了互斥对象,调用者将进入睡眠状态。