類內線程及類內函數做線程啓動函數

0.引言

多線程參考:

1.普通的函數
2.函數對象
3.class中的static函數
4.lammda表達式

類內成員函數不能作爲線程的啓動函數.解決:

1.內類開線程

std::thread t1(bind(&Class::func, this));
t1.join();

2.類內函數做線程啓動函數

Class class;
std::thread task01(bind(&Class::func, &class));  
task01.join();

3.std::this_thread::yield 與std::this_thread::sleep_for

  • std::this_thread::yield: 當前線程放棄執行,操作系統調度另一線程繼續執行。即當前線程將未使用完的“CPU時間片”讓給其他線程使用,等其他線程使用完後再與其他線程一起競爭"CPU"。

  • std::this_thread::sleep_for: 表示當前線程休眠一段時間,休眠期間不與其他線程競爭CPU,根據線程需求,等待若干時間。

  • ref

std::this_thread::sleep_for(std::chrono::seconds(10));    //暫停10秒
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章