日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【C/C++开发】C++11 并发指南二(std::thread 详解)

發(fā)布時間:2023/12/18 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C/C++开发】C++11 并发指南二(std::thread 详解) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上一篇博客《C++11 并發(fā)指南一(C++11 多線程初探)》中只是提到了 std::thread 的基本用法,并給出了一個最簡單的例子,本文將稍微詳細(xì)地介紹 std::thread 的用法。

std::thread 在 <thread> 頭文件中聲明,因此使用 std::thread 時需要包含 <thread> 頭文件。

std::thread 構(gòu)造

default (1)initialization (2)copy [deleted] (3)move (4)
thread() noexcept;
template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);
thread (const thread&) = delete;
thread (thread&& x) noexcept;
  • (1). 默認(rèn)構(gòu)造函數(shù),創(chuàng)建一個空的 thread 執(zhí)行對象。
  • (2). 初始化構(gòu)造函數(shù),創(chuàng)建一個 thread對象,該 thread對象可被 joinable,新產(chǎn)生的線程會調(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í)行對象。
  • 注意:可被 joinable 的 thread 對象必須在他們銷毀之前被主線程 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 threadstd::thread t2(f1, n + 1); // pass by valuestd::thread t3(f2, std::ref(n)); // pass by referencestd::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a threadt2.join();t4.join();std::cout << "Final value of n is " << n << '\n'; }

move 賦值操作

move (1)copy [deleted] (2)
thread& operator= (thread&& rhs) noexcept;
thread& operator= (const thread&) = delete;
  • (1). move 賦值操作,如果當(dāng)前對象不可 joinable,需要傳遞一個右值引用(rhs)給 move 賦值操作;如果當(dāng)前對象可被 joinable,則 terminate() 報錯。
  • (2). 拷貝賦值操作被禁用,thread 對象不可被拷貝。

請看下面的例子:

#include <stdio.h> #include <stdlib.h>#include <chrono> // std::chrono::seconds #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::sleep_for void 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。
  • join
Join 線程。
  • detach
Detach 線程
  • swap
Swap 線程 。
  • native_handle
返回 native handle。
  • hardware_concurrency [static]
檢測硬件并發(fā)特性。

轉(zhuǎn)載于:https://www.cnblogs.com/huty/p/8516998.html

總結(jié)

以上是生活随笔為你收集整理的【C/C++开发】C++11 并发指南二(std::thread 详解)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。