頭文件
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
thread
constructor
thread() noexcept;
thread( thread&& other ) noexcept;
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
thread( const thread& ) = delete;
thread 不可復(fù)制匀奏,沒有兩個(gè) std::thread 對(duì)象可表示同一執(zhí)行線程。
destructor
~thread();
Destroys the thread object.
If *this has an associated thread (joinable() == true), std::terminate() is called.
int main()
{
{
thread th1(t1); // th1離開作用域析構(gòu)后,線程被終止;
cout << std::boolalpha << th1.joinable() << endl; // true
}
while (1);
return 0;
}
operator=
thread& operator=( thread&& other ) noexcept;
thread 不可復(fù)制,沒有兩個(gè) std::thread 對(duì)象可表示同一執(zhí)行線程帖族。
joinable
thread::joinable() 用來判斷線程對(duì)象是否持有一個(gè)活動(dòng)的執(zhí)行線程(執(zhí)行實(shí)例)啥酱。以下情況的線程對(duì)象不持有實(shí)際線程:
- 缺省構(gòu)造的thread對(duì)象
std::thread t1; // t1不是線程
t1.joinable(); // false
/////////////////////////////////////////////////
void f1(int n){
for (int i = 0; i < 2; ++i) {
std::cout << "Thread 1 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
std::thread t2(f1, n + 1);
t2.joinable(); // true
- 調(diào)用了thread::join()的對(duì)象
void t1() //普通的函數(shù)合是,用來執(zhí)行線程
{
for (int i = 0; i < 5; ++i)
{
cout << "t1111" << endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
int main()
{
thread th1(t1);
cout << std::boolalpha << th1.joinable() << endl; // true
th1.join();
cout << std::boolalpha << th1.joinable() << endl;; // false
cout << "here is main\n\n";
while (1);
return 0;
}
- 調(diào)用了thread::detach()的對(duì)象
int main()
{
thread th1(t1);
cout << std::boolalpha << th1.joinable() << endl; // true
th1.detach();
cout << std::boolalpha << th1.joinable() << endl;; // false
cout << "here is main\n\n";
while (1);
return 0;
}
join
調(diào)用該函數(shù)會(huì)阻塞當(dāng)前線程(主調(diào)線程)
阻塞調(diào)用者(caller)所在的線程(主調(diào)線程)直至被join的std::thread對(duì)象標(biāo)識(shí)的線程(被調(diào)線程執(zhí)行結(jié)束。
detach
detach是用來和線程對(duì)象分離的岔绸,這樣線程可以獨(dú)立地執(zhí)行理逊,一旦線程執(zhí)行完畢橡伞,操作系統(tǒng)分配的資源將會(huì)被釋放。不過這樣由于沒有thread對(duì)象指向該線程而失去了對(duì)它的控制晋被。
當(dāng)對(duì)象析構(gòu)時(shí)線程會(huì)繼續(xù)在后臺(tái)執(zhí)行兑徘,但是當(dāng)主程序退出時(shí)并不能保證線程能執(zhí)行完。
如果沒有良好的控制機(jī)制或者這種后臺(tái)線程比較重要羡洛,最好不用detach而應(yīng)該使用join挂脑。
swap
交換兩個(gè)線程對(duì)象所代表的底層句柄
mutex
std::mutex mux;
mux.lock();
mux.unlock();
lock
std::lock_guard , 與Mutex RAII
相關(guān),方便線程對(duì)互斥量上鎖。
std::unique_lock , 與Mutex RAII
相關(guān),方便線程對(duì)互斥量上鎖卫漫,但提供了更好的上鎖和解鎖控制最住。
std::mutex m;
std::lock_guard<mutex> lock1(m);
std::unique_lock<mutex> lock2(m);
condition_variable
#include <iostream>
#include <thread>
#include <stdlib.h>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <queue>
using namespace std;
std::mutex mux;
std::condition_variable cond;
std::queue<int> q;
void producer()
{
for (int i = 0; i < 10; ++i)
{
std::unique_lock<mutex> lock(mux);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
q.push(i);
cond.notify_one();
}
}
void consumer()
{
while (1)
{
std::unique_lock<mutex> lock(mux);
while (q.empty())
{
cond.wait(lock);
}
cout << "consumer " << q.front() << endl;
q.pop();
}
}
int main()
{
std::thread t1(producer);
std::thread t2(consumer);
t1.join();
cout << "producer finished " << endl;
t2.join();
return 0;
}