您好,登錄后才能下訂單哦!
如何理解C++11中的std::thread,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
std::thread 在 <thread> 頭文件中聲明,因此使用 std::thread 時(shí)需要包含 <thread> 頭文件。
std::thread 構(gòu)造
thread() noexcept;
template <class Fn, class... Args>explicit thread (Fn&& fn, Args&&... args);
thread (const thread&) = delete;
thread (thread&& x) noexcept;
default (1) | initialization (2) | copy [deleted] (3) | move (4) |
---|
(1). 默認(rèn)構(gòu)造函數(shù),創(chuàng)建一個(gè)空的 thread 執(zhí)行對(duì)象。(2). 初始化構(gòu)造函數(shù),創(chuàng)建一個(gè) thread對(duì)象,該 thread對(duì)象可被 joinable,新產(chǎn)生的線程會(huì)調(diào)用 fn 函數(shù),該函數(shù)的參數(shù)由 args 給出。(3). 拷貝構(gòu)造函數(shù)(被禁用),意味著 thread 不可被拷貝構(gòu)造。(4). move 構(gòu)造函數(shù),move 構(gòu)造函數(shù),調(diào)用成功之后 x 不代表任何 thread 執(zhí)行對(duì)象。
注意:可被 joinable 的 thread 對(duì)象必須在他們銷毀之前被主線程 join 或者將其設(shè)置為 detached.
std::thread 各種構(gòu)造函數(shù)例子如下(參考):
#include <iostream>#include <utility>#include <thread>#include <chrono>#include <functional>#include <atomic> void f1(int n){ for (int i = 0; i < 5; ++i) { std::cout << "Thread " << n << " executing\n"; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }} void f2(int& n){ for (int i = 0; i < 5; ++i) { std::cout << "Thread 2 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }} int main(){ int n = 0; std::thread t1; // t1 is not a thread std::thread t2(f1, n + 1); // pass by value std::thread t3(f2, std::ref(n)); // pass by reference std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread t2.join(); t4.join(); std::cout << "Final value of n is " << n << '\n';}
move 賦值操作
thread& operator= (thread&& rhs) noexcept;
thread& operator= (const thread&) = delete;
move (1) | copy [deleted] (2) |
---|
(1). move 賦值操作,如果當(dāng)前對(duì)象不可 joinable,需要傳遞一個(gè)右值引用(rhs)給 move 賦值操作;如果當(dāng)前對(duì)象可被 joinable,則 terminate() 報(bào)錯(cuò)。(2). 拷貝賦值操作被禁用,thread 對(duì)象不可被拷貝。
請(qǐng)看下面的例子:
#include <stdio.h>#include <stdlib.h>#include <chrono> // std::chrono::seconds#include <iostream> // std::cout#include <thread> // std::thread, std::this_thread::sleep_forvoid thread_task(int n) { std::this_thread::sleep_for(std::chrono::seconds(n)); std::cout << "hello thread " << std::this_thread::get_id() << " paused " << n << " seconds" << std::endl;}/* * === FUNCTION ========================================================= * Name: main * Description: program entry routine. * ======================================================================== */int main(int argc, const char *argv[]){ std::thread threads[5]; std::cout << "Spawning 5 threads...\n"; for (int i = 0; i < 5; i++) { threads[i] = std::thread(thread_task, i + 1); } std::cout << "Done spawning threads! Now wait for them to join\n"; for (auto& t: threads) { t.join(); } std::cout << "All threads joined.\n"; return EXIT_SUCCESS;} /* ---------- end of function main ---------- */
其他成員函數(shù)
get_id獲取線程 ID。
joinable檢查線程是否可被 join。
joinJoin 線程。
detachDetach 線程
swapSwap 線程 。
native_handle返回 native handle。
hardware_concurrency [static]檢測(cè)硬件并發(fā)特性。
關(guān)于如何理解C++11中的std::thread問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。