
2009年6月12日
如果明確地引用一個NULL指針,則該引用是無效的
int *x = 0;
int& y = *x;
這兩行語句會引起一個段錯誤
別外返回局部變量的引用也是無效的
int& getLocalVariable()
{
int x;
return x;
}
posted @
2009-06-12 13:52 L'雙魚 閱讀(163) |
評論 (0) |
編輯 收藏

2009年2月4日
一: 互斥體的概念
Lockable Concept
TimedLockable Concept
SharedLockable Concept
UpgradeLockable Concept
互斥體對象可以保護競賽的數(shù)據(jù)和不同線程間的安全線程同步.線程可以調(diào)用互斥體的成員函數(shù)lock()來取得互斥體的所有權(quán),unlock()釋放所有權(quán).在Boost::thread中,互斥體可以遞歸使用,允許幾個線程同時擁有互斥體的所有權(quán).
Lockable
概念
void lock() //boost::thread_resource_error
if an error
occurs
bool try_lock()//boost::thread_resource_error
if an error
occurs.
void unlock()
Lockable的實現(xiàn)類必須提供以上函數(shù).通過調(diào)用lock()獲得所有權(quán),unlock()釋放所有權(quán).
TimedLockable
概念
bool timed_lock(boost::system_time const& abs_time)
template<typename DurationType> bool timed_lock(DurationType const& rel_time
//boost::thread_resource_error
if an error
occurs.
TimedLockable
概念精練于
Lockable
支持超時嘗試獲鎖,
TimedLockable
的實現(xiàn)類除了實現(xiàn)Lockable類的成員外,還要實現(xiàn)上面兩個函數(shù).
SharedLockable
概念
void lock_shared()
bool try_lock_shared()
bool timed_lock_shared(boost::system_time const& abs_time)
void unlock_shared()
SharedLockable
精練于TimedLockable,用于提供共享所有權(quán)
UpgradeLockable
概念
void lock_upgrade()
void unlock_upgrade()
void unlock_upgrade_and_lock()
void unlock_upgrade_and_lock_shared()
void unlock_and_lock_upgrade()
UpgradeLockable概念是一個完善SharedLockable概念,可以升級的所有權(quán),以及共同的所有權(quán)和獨自擁有。這是一個擴大到多讀
者/單寫模式所提供的SharedLockable概念:一個單一的線程可能升級所有權(quán)的同時,其他有共同的所有權(quán)。線程所有權(quán)可隨時升級, 嘗試擁有獨家所有權(quán)。如果沒有其他線程有共同的所有權(quán),升級完成,立即和線程現(xiàn)在獨家擁有,它必須放棄要求解鎖()一樣,如果它被收購,呼吁鎖()
。
Class template lock_guard
lock_guard(Lockable & m)
lock_guard(Lockable & m,boost::adopt_lock_t)
~lock_guard()
posted @
2009-02-04 19:56 L'雙魚 閱讀(477) |
評論 (0) |
編輯 收藏

2009年1月22日
1.
寄存器
2. 棧
位于常規(guī)內(nèi)存區(qū)
3.堆
多用途內(nèi)存池(通常為對象)
4.靜態(tài)存儲區(qū)
(非對象數(shù)據(jù))
5.固定存儲
6.非內(nèi)存存儲
(流對象和persistent對象)
posted @
2009-01-22 17:12 L'雙魚 閱讀(178) |
評論 (0) |
編輯 收藏
"Evil does seek to maintain power by suppressing the
truth."
"Or by misleading the innocent."
|
|
Spock and McCoy, "And The Children Shall Lead",
stardate 5029.5. |
歸根結(jié)底,所有問題都是列表,或所有問題都是算法
問題空間的元素同它在空間的表述稱之為對象
萬物皆對象
程序就是一組對象,告訴對方該做什么
每個對象都利用別的對象來組建自己的記憶
對象都有類型
所有屬于同一類型的對象都能接受相同的消息
接口只管你能向這個對象發(fā)送什么消息
實現(xiàn)隱藏能減少程序的bug
繼承: 復(fù)用接口
在新類中修改基類的行為稱為覆寫
"只覆寫"基類的行為,稱為替換規(guī)則,是(is-a)關(guān)系
在派生類中添加新接口是(is-like-a)關(guān)系
多態(tài)替換對象可以降低維護軟件成本和增強設(shè)計
OPP語言采用后綁定的概念.
動態(tài)綁定是JAVA的缺省行為
interface是abstract的深化
posted @
2009-01-22 10:01 L'雙魚 閱讀(161) |
評論 (0) |
編輯 收藏
一. 線程啟動
線程的啟動由傳遞一個沒有構(gòu)造函數(shù)的Callable類,之后復(fù)制到內(nèi)存,由最新的線程調(diào)用.
struct callable
{
void operator()();
};
如果該類必須不可復(fù)制,那么可以用boost::ref傳遞一個Callable對象的引用到構(gòu)造中.
boost::thread copies_are_safe()
{
callable x;
return boost::thread(x); //參數(shù)為X的一份拷貝
} // x is destroyed, but the newly-created thread has a copy, so this is OK
boost::thread oops()
{
callable x;
return boost::thread(boost::ref(x)); //參數(shù)為X的引用
} // x is destroyed, but the newly-created thread still has a reference
// this leads to undefined behaviour
線程可以用一個函數(shù)或callable對象為參數(shù)構(gòu)造,用boost::bind來實現(xiàn)
void find_the_question(int the_answer);
boost::thread deep_thought_2(boost::bind(find_the_question,42));
void print();
boost::thread t=boost::thread(&print);
二.線程接合與脫離
當被銷毀時,線程稱為脫離(
detached),當線程為脫離(detached)時,線程繼續(xù)執(zhí)行直到構(gòu)造函數(shù)中函數(shù)或callable對象執(zhí)行完畢,或程式終止.
void print();
boost::thread t(&print);
t.join();//線程銷毀
t.join();//線程已經(jīng)失效,t不指向任何線程,無作用
std::cout<<boolalpha<<t.joinable()<<std::endl;//print false statement
線程的脫離可以明確調(diào)用boost::detach()函數(shù),這種情況下線程為非現(xiàn)脫離線程(now-detached thread),變?yōu)榉蔷€程(
Not-a-Thread).
boost::thread::join() //如果線程為中斷(interrupted),引發(fā)boost::thread_interrupted異常.
boost::thread::detach() //不引發(fā)異常,如果線程不脫離,線程析構(gòu)時調(diào)用.
posted @
2009-01-22 10:01 L'雙魚 閱讀(1963) |
評論 (0) |
編輯 收藏