C++ 11 thread[WINDOWS/LINUX]( C++ 11之後有了std::thread函式庫,需要引入的標頭檔: )


資料來源: https://medium.com/@chingi071/多執行緒-c-thread-9f6e37c7cf32


GITHUB: https://github.com/jash-git/CPP11_thread

範例測試環境: VC++(2015) / CB20.03


PS.LINUX gcc/g++  c++11編譯的語法
    g++ -g -Wall -std=c++11 main.cpp
    gcc -g -Wall -std=c11 main.cpp


CB設定


code ~ 範例03[C++ thread lock 共用變數]

#include 
#include 
#include 
using namespace std;

/*
std::this_thread::sleep_for(std::chrono::seconds(5)); // sleep 5 ’(seconds)
std::this_thread::sleep_for(std::chrono::milliseconds(5)); // sleep 5 ²@¬í(milliseconds)
std::this_thread::sleep_for(std::chrono::microseconds(5)); // sleep 5 ·L¬í(microseconds)
std::this_thread::sleep_for(std::chrono::nanoseconds(5)); // sleep 5 ¯Ç¬í(nanoseconds)
*/
mutex mu;
int gintCount = 0;
void thread_job(int intData)
{
	mu.lock();
	gintCount += intData;
	std::this_thread::sleep_for(std::chrono::seconds(intData-1));
	cout << "thread  " << intData <

相關文章