search: this is where all story begins
gmail: daily use, just another legend
docs: AWESOME! makes doc not just doc
toolbar for Firefox: some great tools to make Firefox even greater
reader: now part of my life, to let me know what's happening around
translate: not accurate, but always help
blog search: specific tool for specific use
notebook: just not suitable for me
calendar: reminding-SMS is really useful, pity that i'm not always a scheduled person
gtalk: simple and clean. great backup for MSN, though not a replacement yet
picasa: offline tool + online holding. perfect match
blogger: really convenient when combining with google docs; really suck when being blocked by GFW!
mobile: reader, search, map... just give me more!
YouTube: not really so atractive among tons of copiers
iGoogle: not so atractive either, sorry, i prefer classic google homepage
page creator: to build a personal web page is always my dream, but...殘念
desktop search: great idea, but hey guys, you can do better
code: for specialists, seems to be better than sourceforge, we'll see...
SketchUp: not really used, but really interested of this move direction
gadget and
OpenSocial: these guys make Javascript so sexy!
earth: thanks for offering a free tour of world
My top 5
Search
Gmail
Docs
Talk btw: I just feel like to speak some english after watching "how i met ur mother", anyway...
http://www.boost.org/doc/libs/1_35_0/doc/html/thread/synchronization.html Mutex概念線程同步最基本的是mutex(mutual exclusion的縮寫)。一個(gè)互斥體一次只允許一個(gè)線程訪問共享區(qū)。當(dāng)一個(gè)線程想要訪問共享區(qū)時(shí),首先要做的就是鎖住(lock)互斥體。如果其他的 線程已經(jīng)鎖住了互斥體,那么就必須先等那個(gè)線程將互斥體解鎖,這樣就保證了同一時(shí)刻只有一個(gè)線程能訪問共享區(qū)域。
Boost.Thread supplies
recursive and
non-recursive mutexes with
exclusive ownership(獨(dú)占) semantics, along with a
shared ownership (共享) (multiple-reader / single-writer) mutex.
Boost.Thread supports four basic concepts for lockable objects:
Lockable
,
exclusive ownership
TimedLockable
,
SharedLockable
,
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
基礎(chǔ)上,允許upgradable ownership(同時(shí)也支持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外,其余的各種復(fù)雜mutex還需要更多代碼實(shí)踐]
Each mutex type implements one or more of these concepts, as do the
various lock types.
Lock Typesboost定義的Lock types為class template,以合適的Lockable object作為模板參數(shù)
lock_guard
- RAII-style的簡(jiǎn)單lock,在ctor中l(wèi)ock,在dtor中unlock
只支持簡(jiǎn)單的Lockable
object
unique_lock
- 比
lock_guard
復(fù)雜在:不僅提供RAII-style的lock,還允許用戶指定是否在ctor中立即lock,意味著可以指定推遲lock(defer acquiring the lock,通過指定defer_lock_t參數(shù)),直到顯式調(diào)用其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。后面幾種對(duì)應(yīng)于不同需求的lock,從名字就可以直觀看出功能,還未試驗(yàn),直接參考api]
Mutex TypesMutex types對(duì)應(yīng)于之前的mutex concepts,目前有:
適用于不同需求
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一般更優(yōu)化
One-time Initialization僅運(yùn)行一次的例程
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[注:還未使用過]
線程局部存儲(chǔ)(中文說明) (
Another one )
典型的應(yīng)用場(chǎng)景是errno。
Boost線程庫(kù)提供了智能指針boost::thread_specific_ptr來訪問本地存儲(chǔ)線程。是跨平臺(tái)的解決方案
http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_local_storage.html [注:還沒有實(shí)踐過,等寫過相應(yīng)代碼后再來補(bǔ)充文檔]
http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_management.html launching threads boost::thread類代表一個(gè)可執(zhí)行的線程(thread of execution)。
A new thread is launched by
passing an object of a callable type that
can be invoked with no parameters to the constructor.
The object is then copied into internal storage, and invoked on the newly-created thread of execution.
If you wish to construct an instance of
boost::thread with a function or callable object that
requires arguments to be supplied, this can be done using
boost::bind.
(thread non-copiable, but movable; object that used to created a thread must callable, if not, use boost::ref)
Joining and detaching
當(dāng)代表一個(gè)可執(zhí)行的線程(
thread of execution)的boost::thread對(duì)象被銷毀時(shí),這個(gè)線程便同時(shí)被
detached. Detached的線程將繼續(xù)運(yùn)行直到線程終止。
也可以顯式調(diào)用(explicitly)一個(gè)boost::thread對(duì)象的detach()方法,這時(shí)這個(gè)線程直接被detach,而這個(gè)boost::thread對(duì)象講不再代表thread of execution,而指
Not-a-Thread
join()用于等待一個(gè)線程結(jié)束。
(timed_join())
Interruption 調(diào)用boost::thread對(duì)象的
interrupt()方法,可以中斷其對(duì)應(yīng)的線程。
When the interrupted thread next executes one of the specified
interruption points (or if it is currently blocked whilst executing one) with interruption enabled, then a boost::thread_interrupted exception will be thrown in the interrupted thread. If not caught, this will cause the execution of the interrupted thread to terminate. As with any other exception, the stack will be unwound, and destructors for objects of automatic storage duration will be executed.
(boost::this_thread::disable_interruption,
Predefined Interruption Points)
Thread IDs
每一個(gè)運(yùn)行中的thread都有一個(gè)唯一的id值。
調(diào)用對(duì)應(yīng)的
boost::thread對(duì)象的
get_id()方法
,或者在運(yùn)行的thread中調(diào)用
boost::this_thread::get_id()
方法。
Namespace this_thread this_thread下包含的是在正在運(yùn)行的線程內(nèi)部,所能進(jìn)行的線程操作,包括上面提到的get_id()方法
http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread Thread Groupthread_group class provides for a collection of threads that are related in some fashion.
New threads can be added to the group with
add_thread and
create_thread member functions.
thread_group is
not copyable or movable.
基礎(chǔ)概念:
參考:
- http://www.cs.cf.ac.uk/Dave/C/node32.html
一個(gè)c/pthread的thread教程,有一些典型例子的代碼、算法說明
- http://www.stlchina.org/twiki/bin/view.pl/Main/BoostThread#C++%20Boost%20Thread%20%B1%E0%B3%CC%D6%B8%C4%CF
C++ Boost Thread 編程指南(中文版)
http://www.ddj.com/cpp/184401518
英文原版
WARNING:Out-of-Date(2002年的)! 只能參考用
- http://aszt.inf.elte.hu/~gsd/klagenfurt/material/ch03s06.html
英文的一篇boost.thread說明,里面有boost::thread的簡(jiǎn)單例子,其中一個(gè)使用了condition
WARNING:Out-of-Date! 只能參考用
Code: