我們常常會使用std::cout 之類的對象在程序中輸出一些信息,如:
std::cerr << "error: " << "invalid parameter. [" << __FILE__ << ":" << __LINE__ << “]” << std::endl;
這在記錄日誌時比較常用,如果要加入線程安全該怎麼辦呢?線程安全不是指單個的<<操作的安全,而是整個語句的線程安全,一般會通過宏寫成如下之類的型式:
boost::mutex mutex_cout;
#define tscout(x) {boost::mutex::scoped_lock lock(mutex_cout); std::cout << X; }
用起來就像這樣:tscout ( "error: " << "invalid parameter. [" << __FILE__ << ":" << __LINE__ << “]” << std::endl)
其實通過for語句中可以定義區域變量這一特性,完全可以做到不改變使用習慣:
boost::mutex mutex_cout;
#define tscout for(boost::mutex::scoped_lock lock(mutex_cout); lock.locked(); lock.unlock()) std::cout
用法不變:
tscout << "error: " << "invalid parameter. [" << __FILE__ << ":" << __LINE__ << “]” << std::endl;
不改變使用習慣的情況下爲std::stream對象的輸入輸出操作附加線程安全
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
男人如何持久?重現男人本色需從細節入手
SteveSapp9
2022-06-23 11:21:40
aesrdvbrtgnfycjumghki,jokl
bglmqecwg
2022-06-20 03:42:04
Aurelien Tchouameni will try to break the curse of the No 18 shirt inherits Bale number
dogj6613
2022-06-19 16:35:29
Regarder film 4K HD
firdauskomarove
2022-06-19 12:36:40
性愛中怎樣才能持久?多用這些小技巧
SteveSapp9
2022-06-18 15:52:27
Ver Pelicula HD 4K
firdauskomarove
2022-06-18 13:35:49
必利吉吃法,必利吉哪裏買
LouisRodgers5773
2022-06-17 11:18:38
Guarda HD Ultra 4K
firdauskomarove
2022-06-17 08:49:35
dfgshfdg
tenoxop591
2022-06-16 19:51:46
這三個性愛技巧 是所有男人都必須掌握的
SteveSapp9
2022-06-16 15:29:47
HerpaGreens Review - You Read its Ingredients Benefits and Side Effects?
ophnjhas
2022-06-15 17:13:35
adagerb sfvgaerg erag
edsa46457
2022-06-15 02:28:31
男人性功能障礙有何表現
LouisRodgers5773
2022-06-14 12:29:07
DBI\DPI\DSI簡介 (紀錄)
cheweiwu1986
2022-06-14 11:30:42
Biotech Ingredients Market Rising Demand and Future Scope till by 2030
chhidami.a.msg
2022-06-13 20:59:00
24小時熱門文章