如何理解C++11中的std::thread
创始人
2024-12-24 23:01:27
0次

std::thread 在 头文件中声明,因此使用 std::thread 时需要包含 头文件。

std::thread 构造

thread() noexcept;

template explicit thread (Fn&& fn, Args&&... args);

thread (const thread&) = delete;

thread (thread&& x) noexcept;

default (1)initialization (2)copy [deleted] (3)move (4)

(1). 默认构造函数,创建一个空的 thread 执行对象。(2). 初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。(3). 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。(4). move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。

注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

std::thread 各种构造函数例子如下(参考):

#include #include #include #include #include #include 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 赋值操作,如果当前对象不可 joinable,需要传递一个右值引用(rhs)给 move 赋值操作;如果当前对象可被 joinable,则 terminate() 报错。(2). 拷贝赋值操作被禁用,thread 对象不可被拷贝。

请看下面的例子:

#include #include #include  // std::chrono::seconds#include // std::cout#include  // 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 ---------- */

其他成员函数

get_id获取线程 ID。

joinable检查线程是否可被 join。

joinJoin 线程。

detachDetach 线程

swapSwap 线程 。

native_handle返回 native handle。

hardware_concurrency [static]检测硬件并发特性。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...