如何理解C++11中的std::thread

发布时间:2021-10-15 16:36:51 作者:柒染
来源:亿速云 阅读:113

如何理解C++11中的std::thread,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

std::thread 构造

thread() noexcept;

template <class Fn, class... Args>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 <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 赋值操作

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 <stdio.h>#include <stdlib.h>#include <chrono>  // std::chrono::seconds#include <iostream> // std::cout#include <thread>  // 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]检测硬件并发特性。

关于如何理解C++11中的std::thread问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. JAVA和C++区别
  2. 关于c++ 预处理器的案例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ std::thread

上一篇:springboot配置文件的加载顺序是怎样的

下一篇:如何用C#写一个Redis数据同步小工具

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》