http://www.eclipse.org/cdt/downloads.php
GCC最基本的用法是∶gcc [options] [filenames]
但是今天我的實驗結果卻是
g++ testThread.cpp -o test -pthread -std=c++11 這個是正確的
g++ -lpthread -std=c++11 -o test testThread.cpp 可以編譯通過,但是會報
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
這個錯誤
當我把-lphread改爲-phread時,
g++ -pthread -std=c++11 -o test testThread.cpp
編譯通過並能正確運行,所以基本的用法是正確無誤的,自己的一點粗心引出自己對基本用法的懷疑.
關於線程thread
thread對象並不會自動管理線程結束,需要手動控制。常見的控制手段有兩個,join和detach。join用於等待線程結束,而detach的功能是託管線程,線程仍然繼續運行至結束,但不再受到thread對象控制
用std::ref顯式取得對象的引用