一次只能有一个任务持有互斥锁,并且只有这个任务可以解锁互斥锁。
Only one task may hold the mutex at a time, and only this task can unlock the mutex.
基于互斥锁的方法的问题。
这将创建一个新的互斥锁并初始化其结构。
互斥锁很简单,但是有一些规则必须牢记。
The mutex is simple, but there are some rules you should remember.
还要注意,两个条件变量使用相同的互斥锁。
当一个互斥锁被锁定后,它必须被解锁。
在内核中可以使用互斥锁来实现信号量行为。
Mutexes are available in the kernel as a way to accomplish semaphore behavior.
锁定操作的设置涉及分配一个预先锁定的互斥锁。
The setup for a lock operation involves allocation of a prelocked mutex.
可能因为分配的时间片结束,持有互斥锁的线程被取消调度。
A thread holding a mutex can be de-scheduled, perhaps because it was the end of its time-slice.
无论在哪种情形中,当控制被返回时,调用者将持有互斥锁。
In either case, when control is returned, the caller holds the mutex.
因此,最后两个读线程都等待条件变量,互斥锁没有被锁住。
Therefore, at the end of it all, you now have two reader threads, both waiting on the condition variable, and the mutex is unlocked.
比如,多线程的每一个线程可以有一个互斥锁的句柄。
For example, multiple threads can each have a handle to a mutex object.
队列类的构造函数和析构函数负责创建和销毁互斥锁,见清单1。
The constructor and destructor of the queue class are responsible for the creation and destruction of the mutex, as shown in Listing 1.
如果锁可用,则获取锁,执行互斥锁动作,然后释放锁。
When the lock is available, it is taken, the mutually-exclusive action is performed, and then the lock is released.
如果在读线程能够获得互斥锁之前发生了超时,那么不需要进行处理。
If the timeout has occurred before the reader thread could acquire the mutex, then no processing need be done.
非争用的解锁操作将一个锁定的互斥锁返回给线程锁定缓存。
Uncontested unlock operations return a locked mutex to the thread lock cache.
内核互斥锁是在原子api之上实现的,但这对于内核用户是不可见的。
The kernel mutex is implemented on top of the atomic API, though this is not visible to the kernel user.
如果互斥锁被持有(锁定),那么就会返回1;否则,返回0。
If the mutex is held (locked), then one is returned; otherwise, zero.
这对于等待同一互斥锁的其他线程有不利影响,因为等待时间现在会更长。
For other threads waiting on the same mutex, this has a negative effect, because the wait time is now even longer.
最后,可以通过调用mutex_is_locked检查互斥锁的状态。
Finally, you can check the status of a mutex through a call to mutex_is_locked.
如果到超时时间段结束时还没有被唤醒,读线程需要唤醒自身并释放互斥锁。
If not awake otherwise, at the end of the timeout, the reader needs to wake itself up and release the mutex.
可以通过DEFINE _ mutex宏使用一个操作创建和初始化互斥锁。
You create and initialize a mutex in one operation through the DEFINE_MUTEX macro.
更好的做法是定义另一个方法,它接收一系列整数,只获取并释放互斥锁一次。
A far better approach would be to define another method that accepts a list of integers and acquire and release the mutex only once.
这个调用在互斥锁可用时返回,否则,在互斥锁锁可用之前它将休眠。
This call returns if the mutex is available; otherwise, it sleeps until the mutex is available.
多个控制线程可以同时在队列中添加数据或删除数据,所以需要用互斥锁对象管理同步。
Multiple threads of control can simultaneously try to push data to the queue or remove data, so you need a mutex object to manage the synchronization.
现在只剩下两个次要的任务 ——初始化互斥锁和启动线程运行可上传的文件的列表。
This just leaves us with a couple of minor bookkeeping tasks -- initializing the mutex and starting a thread to run through the list of uploadable files.
存放数据的时候,他先要获得互斥锁,再往总线上写数据,最后再释放互斥锁。
When publishing its data, it would acquire a mutex, do writes to the bus, and release 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.
显然,把数据放到队列中就像是把数据添加到列表中,必须使用互斥锁保护这个操作。
Clearly, pushing data into the queue is akin to appending data to the list, and this operation must be guarded by mutex locks.
在两个级别上执行锁定(见清单7):链表有一个读写锁,各个节点包含一个互斥锁。
Locking occurs on two levels (see Listing 7) : The list has a read-write lock, while individual nodes contain a mutex.
应用推荐