http://www.boost.org/doc/libs/1_35_0/doc/html/thread/synchronization.html

Mutex概念
线程同步最基本的是mutex(mutual exclusion的缩写)。一个互斥体一次只允许一个线程访问共享区。当一个线程想要访问共享区时,首先要做的就是锁住(lock)互斥体。如果其他的 线程已经锁住了互斥体,那么就必须先等那个线程将互斥体解锁,这样就保证了同一时刻只有一个线程能访问共享区域。

Boost.Thread supplies recursive and non-recursive mutexes with exclusive ownership(独占) semantics, along with a shared ownership (共享) (multiple-reader / single-writer) mutex.
Boost.Thread supports four basic concepts for lockable objects:

Lockable,
exclusive ownership
TimedLockable,
Lockable的基础上加上了timeout
SharedLockable,
TimedLockable基础上,允许shared ownership(同时也支持exclusive)
This is the standard multiple-reader / single-write model:
at most one thread can have exclusive ownership, and if any thread does have exclusive ownership, no other threads can have shared or exclusive ownership. Alternatively, many threads may have shared ownership.
UpgradeLockable,
SharedLockable基础上,允许upgradable ownership(同时也支持shared、exclusive)
This is an extension to the multiple-reader / single-write model provided by the SharedLockable concept:
a single thread may have upgradable ownership at the same time as others have shared ownership. The thread with upgradable ownership may at any time attempt to upgrade that ownership to exclusive ownership. If no other threads have shared ownership, the upgrade is completed immediately, and the thread now has exclusive ownership, which must be relinquished by a call to unlock(), just as if it had been acquired by a call to lock().

[注:除Lockable的mutex外,其余的各种复杂mutex还需要更多代码实践]

Each mutex type implements one or more of these concepts, as do the various lock types.

Lock Types
boost定义的Lock types为class template,以合适的Lockable object作为模板参数
lock_guard
RAII-style的简单lock,在ctor中lock,在dtor中unlock
只支持简单的Lockable object
unique_lock
lock_guard复杂在:不仅提供RAII-style的lock,还允许用户指定是否在ctor中立即lock,意味着可以指定推迟lock(defer acquiring the lock,通过指定defer_lock_t参数),直到显式调用其lock()方法
还支持TimedLockable concept,前提是需要lock的Lockable object本身支持
The member functions of boost::unique_lock are not thread-safe...[注:这句没看懂。。。]

shared_lock
upgrade_lock
upgrade_to_unique_lock

[注:目前只用过unique_lock。后面几种对应于不同需求的lock,从名字就可以直观看出功能,还未试验,直接参考api]
Mutex Types
Mutex types对应于之前的mutex concepts,目前有:
Class mutex
boost::mutex实现了Lockable concept,提供exclusive-ownership mutex.
At most one thread can own the lock on a given instance of boost::mutex at any time.
Multiple concurrent calls to lock(), try_lock() and unlock() shall be permitted.

typedef unique_lock mutex::scoped_lock;

Typedef try_mutex
Class timed_mutex
Class recursive_mutex
Typedef recursive_try_mutex
Class recursive_timed_mutex
Class shared_mutex
适用于不同需求


Condition Variables
The general usage pattern is that one thread locks a mutex and then calls wait on an instance of condition_variable or condition_variable_any. When the thread is woken from the wait, then it checks to see if the appropriate condition is now true, and continues if so. If the condition is not true, then the thread then calls wait again to resume waiting.(中文参考)

lock is passed to wait()wait() will atomically add the thread to the set of threads waiting on the condition variable, and unlock the mutex. When the thread is woken, the mutex will be locked again before the call to wait returns. This allows other threads to acquire the mutex in order to update the shared data, and ensures that the data associated with the condition is correctly synchronized.

In the mean time, another thread sets the condition to true, and then calls either notify_one() or notify_all() on the condition variable to wake one waiting thread or all the waiting threads respectively.


condition_variable_any比condition_variable更通用;condition_variable要求传给wait()的必须是boost::unique_lock<boost::mutex>类型;condition_variable一般更优化

One-time Initialization
仅运行一次的例程
http://www.stlchina.org/twiki/bin/view.pl/Main/BoostThread#5%20%BD%F6%D4%CB%D0%D0%D2%BB%B4%CE%B5%C4%C0%FD%B3%CC
[注:还未使用过]

Barriers
[注:还未使用过]



posted on 2008-04-29 14:28 frank28_nfls 阅读(626) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理