互斥量对象必须先被声明;声明后还必须初始化。
The mutex object must be declared; once declared, it must be initialized.
现在,有两个线程需要使用这两个互斥量。
Now, imagine that two threads want to use them. Thread one does this.
占有这个惟一物体的过程就叫做锁定或者获得互斥量。
Once you have it, no one can take it away from you until you put it down. The process of picking up the unique object is called locking, or acquiring, the mutex.
如果可能,在设计程序时决不要锁定一个已经锁定的互斥量。
If possible, just design your program never to re-lock a mutex it already has.
所以,让我们更进一步,在产生随机数的地方加一个互斥量。
So, let's say we go ahead and add a mutex that surrounds the random number generator.
主线程:锁定互斥量并增量threadcount。
像这样的问题,一个简单的解决办法是保证以相同的顺序获得互斥量。
One simple solution to problems like this is to ensure that locks are always acquired in the same order.
创建和使用互斥量的过程比仅仅是开始一个线程的过程要稍微复杂一些。
The procedure for creating and using a mutex is a bit more complicated than the procedure for starting a thread.
当pthread_cond_wait被调用后,它解锁互斥量并停止线程的执行。
When invoked, pthread_cond_wait unlocks the mutex and then pauses execution of its thread.
这个例程做了一些新的工作;它锁定一个叫做count _ mutex的互斥量。
This routine does something new; it locks a mutex called, creatively enough, count_mutex.
另外,您还可以使用“递归”类型的互斥量,这种互斥量允许对同一个互斥量锁定多次。
Otherwise, you can use the "recursive" mutex type, which allows the holder of the lock to lock the same mutex multiple times.
之后它创建一个新的线程同时增量threadcount;在完成之后,它解锁互斥量。
After locking that, it creates a new thread and increments threadcount; after it's done, it unlocks the mutex.
如果线程2要运行,它就要锁定count _ mutex,而这个互斥量已经被线程1占有了。
If thread two runs, it'll want to lock count_mutex, which is held by thread one.
如果线程1要运行,它就要锁定rand _ mutex,可是这个互斥量已经被线程2阻塞了。
If thread one runs, it'll want to lock rand_mutex, which is held by thread two.
如果互斥量threadcount的值减小到了零,我们知道这时已经没有线程在运行了,该退出程序了。
If the decrement leaves threadcount at zero, we know that there are no threads running, so it's time to exit.
当不再使用互斥量时,可以调用pthread _ mutex_destroy来释放在初始化过程中分配的资源。
When you're done with a mutex, you can release any resources allocated during initialization with a call to pthread_mutex_destroy .
这个坏程序的开发者起初的想法是好的,即只在真正需要使用之前才锁定这些互斥量,但是他却直到运行结束才解锁。
The hapless developer of this monstrosity came up with the clever idea of only locking things when they are actually about to be used, but deferring unlocking until the end of a run.
最后,当一个线程从调用pthread_cond_wait而被唤醒时,要做的第一件事就是重新锁定它在最初调用时解锁的那个互斥量。
Finally, the first thing any thread tries to do when waking up from pthread_cond_wait is re-lock the mutex it unlocked when initially called.
如果您将调用pthread_create前面改变threadcount值的代码去掉,那么互斥量代码中间就只剩下减小计数值的语句了。
If you moved the change in threadcount in front of the call to pthread_create , then the mutex code could just surround the actual decrement.
函数pthread_cond_wait主要就是用于阻塞线程的,它有两个参数;第一个是一个指向条件变量的指针,第二个是一个锁定了的互斥量。
The function primarily used for this is pthread_cond_wait . It takes two arguments; the first is a pointer to a condition variable, and the second is a locked mutex.
如果调用线程并不拥有这个互斥信号量,那么这个函数的执行将会失败。
If the calling thread has no ownership of this mutex, this function fails.
这个方法允许您设置互斥信号量对象的相关属性。
在内核中可以使用互斥锁来实现信号量行为。
Mutexes are available in the kernel as a way to accomplish semaphore behavior.
要释放一个不再需要的互斥信号量对象,可以使用pthread _ mutex_destroy 。
To free a mutex object that is no longer needed, pthread_mutex_destroy is used.
当拥有这个互斥信号量的线程不再需要它的时候,可以调用ReleaseMutex,以便将它释放回系统。
ReleaseMutex is called when the owning thread no longer needs the mutex, and it can be conveniently released to the system.
基本知识信号量,互斥和线程是必需的。
Basic knowledge of semaphores, Mutex, and threads are required.
在厕所里重要的例子二进制信号量互斥的区别?
Difference between Binary Semaphore and Mutex in the toilet key example?
希望看到这里,你应该对二进制信号量,计数信号量和互斥锁的区别有了清晰的认识。
Hopefully you should now be clear about the core differences between the Binary Semaphore, General (counting) Semaphore and the Mutex.
希望看到这里,你应该对二进制信号量,计数信号量和互斥锁的区别有了清晰的认识。
Hopefully you should now be clear about the core differences between the Binary Semaphore, General (counting) Semaphore and the Mutex.
应用推荐