溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++中thread的使用方法

發(fā)布時間:2020-07-23 11:54:54 來源:億速云 閱讀:356 作者:小豬 欄目:開發(fā)技術

這篇文章主要講解了C++中thread的使用方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

1,簡介

C++11中加入了<thread>頭文件,此頭文件主要聲明了std::thread線程類。C++11的標準類std::thread對線程進行了封裝,定義了C++11標準中的一些表示線程的類、用于互斥訪問的類與方法等。應用C++11中的std::thread便于多線程程序的移值。

std::thread類成員函數(shù):

(1)、get_id:獲取線程ID,返回一個類型為std::thread::id的對象。

(2)、joinable:檢查線程是否可被join。檢查thread對象是否標識一個活動(active)的可行性線程。缺省構造的thread對象、已經(jīng)完成join的thread對象、已經(jīng)detach的thread對象都不是joinable。

(3)、join:調用該函數(shù)會阻塞當前線程。阻塞調用者(caller)所在的線程直至被join的std::thread對象標識的線程執(zhí)行結束。

(4)、detach:將當前線程對象所代表的執(zhí)行實例與該線程對象分離,使得線程的執(zhí)行可以單獨進行。一旦線程執(zhí)行完畢,它所分配的資源將會被釋放。

(5)、native_handle:該函數(shù)返回與std::thread具體實現(xiàn)相關的線程句柄。native_handle_type是連接thread類和操作系統(tǒng)SDK API之間的橋梁,如在Linux g++(libstdc++)里,native_handle_type其實就是pthread里面的pthread_t類型,當thread類的功能不能滿足我們的要求的時候(比如改變某個線程的優(yōu)先級),可以通過thread類實例的native_handle()返回值作為參數(shù)來調用相關的pthread函數(shù)達到目錄。This member function is only present in class thread if the library implementation supports it. If present, it returns a value used to access implementation-specific information associated to the thread.

(6)、swap:交換兩個線程對象所代表的底層句柄。

(7)、operator=:moves the thread object

(8)、hardware_concurrency:靜態(tài)成員函數(shù),返回當前計算機最大的硬件并發(fā)線程數(shù)目?;旧峡梢砸暈樘幚砥鞯暮诵臄?shù)目。

另外,std::thread::id表示線程ID,定義了在運行時操作系統(tǒng)內(nèi)唯一能夠標識該線程的標識符,同時其值還能指示所標識的線程的狀態(tài)。Values of this type are returned by thread::get_id and this_thread::get_id to identify threads.

有時候我們需要在線程執(zhí)行代碼里面對當前調用者線程進行操作,針對這種情況,C++11里面專門定義了一個命名空間this_thread,此命名空間也聲明在<thread>頭文件中,其中包括get_id()函數(shù)用來獲取當前調用者線程的ID;yield()函數(shù)可以用來將調用者線程跳出運行狀態(tài),重新交給操作系統(tǒng)進行調度,即當前線程放棄執(zhí)行,操作系統(tǒng)調度另一線程繼續(xù)執(zhí)行;sleep_until()函數(shù)是將線程休眠至某個指定的時刻(time point),該線程才被重新喚醒;sleep_for()函數(shù)是將線程休眠某個指定的時間片(time span),該線程才被重新喚醒,不過由于線程調度等原因,實際休眠實際可能比sleep_duration所表示的時間片更長。

1.創(chuàng)建一個線程

創(chuàng)建線程比較簡單,使用std的thread實例化一個線程對象就創(chuàng)建完成了,示例:

#include <iostream>
#include <thread>
#include <stdlib.h> //sleep
 
using namespace std;
 
void t1() //普通的函數(shù),用來執(zhí)行線程
{
  for (int i = 0; i < 10; ++i)
  {
    cout << "t1111\n";
    sleep(1);
  }
}
void t2()
{
  for (int i = 0; i < 20; ++i)
  {
    cout << "t22222\n";
    sleep(1);
  }
}
int main()
{
  thread th2(t1); //實例化一個線程對象th2,使用函數(shù)t1構造,然后該線程就開始執(zhí)行了(t1())
  thread th3(t2);
 
  th2.join(); // 必須將線程join或者detach 等待子線程結束主進程才可以退出
  th3.join(); 
 
  //or use detach
  //th2.detach();
  //th3.detach();
 
  cout << "here is main\n\n";
 
  return 0;
}

上述提到的問題,還可以使用detach來解決,detach是用來和線程對象分離的,這樣線程可以獨立地執(zhí)行,不過這樣由于沒有thread對象指向該線程而失去了對它的控制,當對象析構時線程會繼續(xù)在后臺執(zhí)行,但是當主程序退出時并不能保證線程能執(zhí)行完。如果沒有良好的控制機制或者這種后臺線程比較重要,最好不用detach而應該使用join。

2, mutex和std::lock_guard的使用

頭文件是#include <mutex>,mutex是用來保證線程同步的,防止不同的線程同時操作同一個共享數(shù)據(jù)。

但使用lock_guard則相對安全,它是基于作用域的,能夠自解鎖,當該對象創(chuàng)建時,它會像m.lock()一樣獲得互斥鎖,當生命周期結束時,它會自動析構(unlock),不會因為某個線程異常退出而影響其他線程。示例:

#include <iostream>
#include <thread>
#include <mutex>
#include <stdlib.h>
 
int cnt = 20;
std::mutex m;
void t1()
{
  while (cnt > 0)
  {  
    std::lock_guard<std::mutex> lockGuard(m);
    // std::m.lock();
    if (cnt > 0)
    {
      //sleep(1);
      --cnt;
      std::cout << cnt << std::endl;
    }
    // std::m.unlock();
    
  }
}
void t2()
{
  while (cnt > 0)
  {
    std::lock_guard<std::mutex> lockGuard(m);
    // std::m.lock();
    if (cnt > 0)
    {
      --cnt;
      std::cout << cnt << std::endl;
    }
    // std::m.unlock();
  }
}
 
int main(void)
{
 std::thread th2(t1);
 std::thread th3(t2);
 
 th2.join();  //等待t1退出
 th3.join();  //等待t2退出
 
 std::cout << "here is the main()" << std::endl;
 
 return 0;
}

輸出結果,cnt是依次遞減的,沒有因為多線程而打亂次序::

19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
here is the main()

看完上述內(nèi)容,是不是對C++中thread的使用方法有進一步的了解,如果還想學習更多內(nèi)容,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI