C++11并发编程:多线程std::thread

 更新时间:2018年12月21日 08:35:38   作者:蜗牛201  
今天小编就为大家分享一篇关于C++11并发编程:多线程std::thread,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

一:概述

C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改。现在在C++11中只需使用语言层面的thread可以解决这个问题。

所需头文件<thread>

二:构造函数

1.默认构造函数

  • thread() noexcept
  • 一个空的std::thread执行对象

2.初始化构造函数

template<class Fn, class... Args>

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

创建std::thread执行对象,线程调用threadFun函数,函数参数为args。

void threadFun(int a)
{
  cout << "this is thread fun !" << endl;
}
thread t1(threadFun, 2);

3.拷贝构造函数

thread(const thread&) = delete;

拷贝构造函数被禁用,std::thread对象不可拷贝构造

void threadFun(int& a)
{
  cout << "this is thread fun !" << endl;
}  
int value = 2;
thread t1(threadFun, std::ref(value));

4.Move构造函数

thread(thread&& x)noexcept

调用成功原来x不再是std::thread对象

void threadFun(int& a)
{
  cout << "this is thread fun !" << endl;
} 
int value = 2;
thread t1(threadFun, std::ref(value));
thread t2(std::move(t1));
t2.join();

三:成员函数

1.get_id()

获取线程ID,返回类型std::thread::id对象。

thread t1(threadFun);
thread::id threadId = t1.get_id();
cout << "线程ID:" << threadId << endl;
//threadId转换成整形值,所需头文件<sstream>
ostringstream  oss;
oss << t1.get_id();
string strId = oss.str();
unsigned long long tid = stoull(strId);
cout << "线程ID:" << tid << endl;

2.join()

创建线程执行线程函数,调用该函数会阻塞当前线程,直到线程执行完join才返回。

thread t1(threadFun);
t1.join() //阻塞等待

3.detach()

detach调用之后,目标线程就成为了守护线程,驻留后台运行,与之关联的std::thread对象失去对目标线程的关联,无法再通过std::thread对象取得该线程的控制权。

4.swap()

交换两个线程对象

thread t1(threadFun1);
thread t2(threadFun2);
cout << "线程1的ID:" << t1.get_id() << endl;
cout << "线程2的ID:" << t2.get_id() << endl;
t1.swap(t2);
cout << "线程1的ID:" << t1.get_id() << endl;
cout << "线程2的ID:" << t2.get_id() << endl;

5.hardware_concurrency()

获得逻辑处理器储量,返回值为int型

int coreNum = thread::hardware_concurrency();

四:使用

1.创建线程

void threadFun1()
{
  cout << "this is thread fun1 !" << endl;
}
int main()
{
  thread t1(threadFun1);
  t1.join();
  getchar();
  return 1;
}

2.创建线程,传参

void threadFun1(int v)
{
  cout << "this is thread fun1 !" << endl;
  cout << v << endl;
}
int main()
{
  int value = 6;
  thread t1(threadFun1, value);
  t1.join();
  getchar();
  return 1;
}

需要注意,变量int value 和int v 做变量传递时并不是引用,而是对变量做了拷贝,所以在传递给int v前,int value不能出作用域(释放了内存),join(),可以保证int value变量释放内存,如果使用detach(),可能存在这种情况。

3.创建线程,引用传参

void threadFun1(int& v)
{
  cout << "this is thread fun1 !" << endl;
  cout << v << endl;
}
int main()
{
  int value = 6;
  thread t1(threadFun1, std::ref(value));
  t1.join();
  getchar();
  return 1;
}

4.创建建线程,线程函数为类成员函数

class Object
{
public:
  Object()
  {
    cout << "构造函数" << endl;
  }
  ~Object()
  {
    cout << "析构函数" << endl;
  }
  void fun(string info)
  {
    cout << info << endl;
  }
};
int main()
{
  Object obj;
  string str = "我是一个类的成员函数!";
  thread t1(&Object::fun, &obj, str);
  t1.join();
  getchar();
  return 1;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • C++操作文件进行读取、删除、修改指定行

    C++操作文件进行读取、删除、修改指定行

    今天小编就为大家分享一篇关于C++操作文件进行读取、删除、修改指定行,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 如何在二叉树中找出和为某一值的所有路径

    如何在二叉树中找出和为某一值的所有路径

    本篇文章是对在二叉树中找出和为某一值的所有路径方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言练习之数组中素数交换

    C语言练习之数组中素数交换

    这篇文章主要为大家介绍了C语言数组中素数交换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助<BR>
    2021-12-12
  • Windows10配置VSCode C++环境(超详细,面向小白以及大佬们)

    Windows10配置VSCode C++环境(超详细,面向小白以及大佬们)

    这篇文章主要介绍了Windows10配置VSCode C++环境(超详细,面向小白以及大佬们),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • C++实现LeetCode(173.二叉搜索树迭代器)

    C++实现LeetCode(173.二叉搜索树迭代器)

    这篇文章主要介绍了C++实现LeetCode(173.二叉搜索树迭代器),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C语言实现消消乐游戏的代码分享

    C语言实现消消乐游戏的代码分享

    本章我们将编写十字消除游戏,用户点击空白方块,沿其上下左右方向寻找第一个彩色方块,如果有两个或两个以上颜色一致,就将其消除,感兴趣的可以了解一下
    2023-02-02
  • Visual Studio2022+QT6创建桌面应用实现

    Visual Studio2022+QT6创建桌面应用实现

    本文主要介绍了Visual Studio2022+QT6创建桌面应用实现,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-02-02
  • Microsoft Visual C++ 安装失败 0x80070666的问题解决

    Microsoft Visual C++ 安装失败 0x80070666的问题解

    本文主要介绍了Microsoft Visual C++ 安装失败 0x80070666的问题解决,错误可能由已安装其他VisualC++版本、VisualC++安装异常、Windows更新计划安装同一VisualC++包等原因引起,下面就来介绍一下解决方案,感兴趣的可以了解一下
    2025-03-03
  • C语言宏函数container of()简介

    C语言宏函数container of()简介

    这篇文章介绍了C语言宏函数container of(),对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • C语言中pthread_exit()函数实现终止线程

    C语言中pthread_exit()函数实现终止线程

    本文主要介绍了C语言中pthread_exit()函数实现终止线程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05

最新评论