欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++11 并發(fā)指南之std::thread 詳解

 更新時間:2020年02月05日 10:01:34   作者:Haippy  
這篇文章主要介紹了C++11 并發(fā)指南之std::thread 詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

std::thread 構(gòu)造

default (1)
thread() noexcept;
initialization (2)
template <class Fn, class... Args>
explicit thread (Fn&& fn, Args&&... args);
copy [deleted] (3)
thread (const thread&) = delete;
move (4)
thread (thread&& x) noexcept;

(1). 默認構(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 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 賦值操作

move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;

(1). move 賦值操作,如果當前對象不可 joinable,需要傳遞一個右值引用(rhs)給 move 賦值操作;如果當前對象可被 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ā)特性。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言二叉樹層序遍歷

    C語言二叉樹層序遍歷

    這篇文章主要介紹了C語言二叉樹層序遍歷,文章基于C語言的相關(guān)資料展開詳細的文章內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學習有所幫助
    2022-04-04
  • C語言實現(xiàn)BF算法案例詳解

    C語言實現(xiàn)BF算法案例詳解

    這篇文章主要介紹了C語言實現(xiàn)BF算法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 基于C++編寫一個簡單的服務(wù)器

    基于C++編寫一個簡單的服務(wù)器

    這篇文章主要為大家詳細介紹了如何基于C++編寫一個簡單的服務(wù)器,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以了解一下
    2023-03-03
  • C語言編程之掃雷小游戲空白展開算法優(yōu)化

    C語言編程之掃雷小游戲空白展開算法優(yōu)化

    掃雷是電腦上很經(jīng)典的游戲,特意去網(wǎng)上玩了一會,幾次調(diào)試之后,發(fā)現(xiàn)這個比三子棋要復(fù)雜一些,尤其是空白展開算法上和堵截玩家有的一拼,與實際游戲差別較大,不能使用光標,下面來詳解每一步分析
    2021-09-09
  • C語言中#pragma預(yù)處理指令的使用

    C語言中#pragma預(yù)處理指令的使用

    在所有的預(yù)處理指令中,#pragma指令可能是最復(fù)雜的了,它的作用是設(shè)定編譯器的狀態(tài)或者是指示編譯器完成一些特定的動作,本文主要介紹了C語言中#pragma預(yù)處理指令的使用,感興趣的可以了解一下
    2023-12-12
  • C++中實現(xiàn)隊列類鏈式存儲與棧類鏈式存儲的代碼示例

    C++中實現(xiàn)隊列類鏈式存儲與棧類鏈式存儲的代碼示例

    這篇文章主要介紹了C++中實現(xiàn)隊列類鏈式存儲與棧類鏈式存儲的代碼示例,通過注釋來說明,直接上代碼,簡單粗暴XD 需要的朋友可以參考下
    2016-03-03
  • 詳解c++中的trait與policy模板技術(shù)

    詳解c++中的trait與policy模板技術(shù)

    trait模板和policy模板技術(shù)是把模板的trait和policy這兩個針對不同具體類型有變化的方面抽離出來形成兩個獨立的模板。由于trait和policy本身是模板,它的行為是可配置的,在模板中通過組合或者以模板實參傳進來的方式使用trait和policy,就可以配置出不同的具體實現(xiàn)
    2021-06-06
  • C程序中可怕的野指針圖文詳解

    C程序中可怕的野指針圖文詳解

    這篇文章主要給大家介紹了關(guān)于C程序中可怕的野指針的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用C程序具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • C++11中列表初始化機制的概念與實例詳解

    C++11中列表初始化機制的概念與實例詳解

    在我們實際編程中,我們經(jīng)常會碰到變量初始化的問題,對于不同的變量初始化的手段多種多樣,下面這篇文章主要給大家介紹了關(guān)于C++11中列表初始化機制的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • C語言中遞歸的實際應(yīng)用與經(jīng)典問題

    C語言中遞歸的實際應(yīng)用與經(jīng)典問題

    函數(shù)以及函數(shù)的遞歸調(diào)用是學習C語言必須要掌握的內(nèi)容,且遞歸作為經(jīng)典的算法思想被廣泛應(yīng)用于程序設(shè)計中,下面這篇文章主要給大家介紹了關(guān)于C語言中遞歸的實際應(yīng)用與經(jīng)典問題的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評論