一:概述

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, );

3.拷贝构造函数

thread(const thread&) = delete;
拷贝构造函数被禁用,std::thread对象不可拷贝构造

 void threadFun(int& a)
{
cout << "this is thread fun !" << endl;
} int value = ;
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 = ;
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 ;
}

2.创建线程,传参

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

需要注意,变量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 = ;
thread t1(threadFun1, std::ref(value));
t1.join(); getchar();
return ;
}

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 ;
}

扫码关注公众号

专注分享C/C++,C++(11,14,17),STL,Java,Spring,mybatis,mysql,redis,分布式,高并发,设计模式,爬虫,docker,shell编程等相关技术,还有高薪互联网职位内推,在这里一起探讨,一起学习,一起进步,同时不定期分享视频书籍资源,充分利用碎片化时间,让我们的技术之路更加有乐趣!

C++11并发编程:多线程std::thread的更多相关文章

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

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

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

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

  3. 【C/C++开发】C++11 并发指南二(std::thread 详解)

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

  4. C++11 并发指南五(std::condition_variable 详解)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

  5. C++11 并发指南五(std::condition_variable 详解)(转)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

  6. 【转】C++11 并发指南五(std::condition_variable 详解)

    http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...

  7. 【C/C++开发】C++11 并发指南三(std::mutex 详解)

    本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...

  8. C++11 并发指南三(std::mutex 详解)

    上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...

  9. C++11 并发指南三(std::mutex 详解)(转)

    转自:http://www.cnblogs.com/haippy/p/3237213.html 上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::th ...

  10. C++11 并发编程库

    C++11 并发编程 C++11 新标准中引入了几个头文件来支持多线程编程,他们分别是: <atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_f ...

随机推荐

  1. CodeForces 327E Axis Walking(状压DP+卡常技巧)

    Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...

  2. 安装Python-Windows

    安装Python-Windows 在开始Python编程前,需要先安装Python环境.Python安装包可以到Python的官网下载,官网地址是https://www.python.org/,如果想 ...

  3. iOS AppStore个人开发者账号申请

    一.申请Apple Developer账号 1.注册App ID 1.打开苹果开发者网页,选择Account,注册Apple ID.   2.填写注册信息 3.地区选择China,填写好验证码,点击C ...

  4. Transcation And Lock--SQL SERVER 事务隔离级别

    SQL SERVER 事务隔离级别:1.未提交读(READ UNCOMMITED)    允许脏读,读取数据时不加共享锁,与使用WITH(NOLOCK)结果相同2.已提交读    不允许脏读,读取数据 ...

  5. webUploader 的使用

    地址:http://fex.baidu.com/webuploader/demo.html 这个主要是扒的demo 引用<link href="~/Scripts/webuploade ...

  6. scrapy-redis3

    原文链接:scrapy-redis使用以及剖析

  7. django 配置xadmin

    django xadmin本地安装 百度云 下载,激活码:bxhv,下载后不需要解压,直接本地 pip install xxx.zip django 版本需要 1.1.11, 1,添加app INST ...

  8. Oracle 中wmsys.wm_concat拼接字符串,结果过长报错解决

    备忘:这个函数最大是4000,根据拼接列的长度,通过限制拼接条数来防止拼接字符串过长错误 --这个情况是从子表中读取出具,这里直接把它当做查询字段处理,在子表中有所有数据 select info.id ...

  9. Ionic——下一代 APP 开发框架

    http://www.tuicool.com/articles/iY3ENvY 最近 Facebook React 团队释出了 React Native, 用来构建 Mobile Native 应用. ...

  10. NOIP模拟题汇总(加厚版)

    \(NOIP\)模拟题汇总(加厚版) T1 string 描述 有一个仅由 '0' 和 '1' 组成的字符串 \(A\),可以对其执行下列两个操作: 删除 \(A\)中的第一个字符: 若 \(A\)中 ...