C++11 并发指南二(std::thread 详解)(转)

上一篇博客《C++11 并发指南一(C++11 多线程初探)》中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用法。

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

std::thread 构造

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). 默认构造函数,创建一个空的 thread 执行对象。
  • (2). 初始化构造函数,创建一个 thread对象,该 thread对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
  • (3). 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
  • (4). move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。
  • 注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

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

 1 #include <iostream>
 2 #include <utility>
 3 #include <thread>
 4 #include <chrono>
 5 #include <functional>
 6 #include <atomic>
 7  
 8 void f1(int n)
 9 {
10     for (int i = 0; i < 5; ++i) {
11         std::cout << "Thread " << n << " executing
";
12         std::this_thread::sleep_for(std::chrono::milliseconds(10));
13     }
14 }
15  
16 void f2(int& n)
17 {
18     for (int i = 0; i < 5; ++i) {
19         std::cout << "Thread 2 executing
";
20         ++n;
21         std::this_thread::sleep_for(std::chrono::milliseconds(10));
22     }
23 }
24  
25 int main()
26 {
27     int n = 0;
28     std::thread t1; // t1 is not a thread
29     std::thread t2(f1, n + 1); // pass by value
30     std::thread t3(f2, std::ref(n)); // pass by reference
31     std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
32     t2.join();
33     t4.join();
34     std::cout << "Final value of n is " << n << '
';
35 }

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 对象不可被拷贝。

请看下面的例子:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #include <chrono>    // std::chrono::seconds
 5 #include <iostream>  // std::cout
 6 #include <thread>    // std::thread, std::this_thread::sleep_for
 7 
 8 void thread_task(int n) {
 9     std::this_thread::sleep_for(std::chrono::seconds(n));
10     std::cout << "hello thread "
11         << std::this_thread::get_id()
12         << " paused " << n << " seconds" << std::endl;
13 }
14 
15 /*
16  * ===  FUNCTION  =========================================================
17  *         Name:  main
18  *  Description:  program entry routine.
19  * ========================================================================
20  */
21 int main(int argc, const char *argv[])
22 {
23     std::thread threads[5];
24     std::cout << "Spawning 5 threads...
";
25     for (int i = 0; i < 5; i++) {
26         threads[i] = std::thread(thread_task, i + 1);
27     }
28     std::cout << "Done spawning threads! Now wait for them to join
";
29     for (auto& t: threads) {
30         t.join();
31     }
32     std::cout << "All threads joined.
";
33 
34     return EXIT_SUCCESS;
35 }  /* ----------  end of function main  ---------- */

其他成员函数

原文地址:https://www.cnblogs.com/zl1991/p/6993748.html