知识链接:

https://www.cnblogs.com/lidabo/p/7852033.html

构造函数如下:

default ()
thread() noexcept;
initialization()
template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);
copy [deleted] ()
thread (const thread&) = delete;
move []
thread (thread&& x) noexcept;
().默认构造函数,创建一个空的 thread 执行对象。

().初始化构造函数,创建一个 thread 对象,该 thread 对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。

().拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。

().move 构造函数,move 构造函数,调用成功之后 x 不代表任何 thread 执行对象。

注意:可被 joinable 的 thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached
#include<thread>
#include<chrono>
#include <iostream>
using namespace std;
void fun1(int n) //初始化构造函数
{
cout << "Thread " << n << " executing\n";
n += ;
this_thread::sleep_for(chrono::milliseconds());
}
void fun2(int & n) //拷贝构造函数
{
cout << "Thread " << n << " executing\n";
n += ;
this_thread::sleep_for(chrono::milliseconds());
}
int main()
{
int n = ;
thread t1; //t1不是一个thread
thread t2(fun1, n + ); //按照值传递
t2.join();
cout << "n=" << n << '\n';
n = ;
thread t3(fun2, ref(n)); //引用
thread t4(move(t3)); //t4执行t3,t3不是thread
t4.join();
cout << "n=" << n << '\n';
system("pause");
return ;
}
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running()
{
cout << "thread is running..." << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running); // 根据函数初始化执行
thread t2(running);
thread t3(running); // 线程数组
thread th[] {thread(running), thread(running), thread(running)}; // 执行 // 堆上
thread* pt1(new thread(running));
thread* pt2(new thread(running));
thread* pt3(new thread(running)); // 线程指针数组
thread* pth(new thread[]{thread(running), thread(running), thread(running)}); return a.exec();
}

多线程传递参数

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running(const char* str,const int id)
{
cout << "thread" << id << "is running..."<< str << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running,"hello1",); // 根据函数初始化执行
thread t2(running,"hello2",);
thread t3(running,"hello3",); return a.exec();
}

join

join 是让当前主线程等待所有的子线程执行完,才能退出。

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running(const char* str,const int id)
{
cout << "thread" << id << "is running..."<< str << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running,"hello1",); // 根据函数初始化执行
thread t2(running,"hello2",);
thread t3(running,"hello3",); cout << t1.joinable() << endl;
cout << t2.joinable() << endl;
cout << t3.joinable() << endl; t1.join(); // 主线程等待当前线程执行完成再退出
t2.join();
t3.join(); return a.exec();
}

detach

线程 detach 脱离主线程的绑定,主线程挂了,子线程不报错,子线程执行完自动退出。
线程 detach以后,子线程会成为孤儿线程,线程之间将无法通信。
#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; void running(const char* str,const int id)
{
cout << "thread" << id << "is running..."<< str << endl;
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 栈上
thread t1(running,"hello1",); // 根据函数初始化执行
thread t2(running,"hello2",);
thread t3(running,"hello3",); cout << t1.joinable() << endl;
cout << t2.joinable() << endl;
cout << t3.joinable() << endl; t1.detach();
t2.detach();
t3.detach(); return a.exec();
}

获取cpu核心个数

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto n = thread::hardware_concurrency();//获取cpu核心个数
cout << n << endl; # return a.exec();
}

CPP原子变量与线程安全。

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
using namespace std; const int N = ;
int num = ; void run()
{
for (int i = ; i < N; ++i){
num++;
}
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); clock_t start = clock(); thread t1(run);
thread t2(run);
t1.join();
t2.join(); clock_t end = clock();
cout << "num=" << num << ",spend time:" << end - start << "ms" << endl; return a.exec();
}

运行结果:num=1157261,spend time:9ms
结果并不是200000,这是由于线程之间的冲突

互斥量

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; const int N = ;
int num = ;
mutex m;
void run()
{
m.lock();
for (int i = ; i < N; ++i){
num++;
}
m.unlock();
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); clock_t start = clock(); thread t1(run);
thread t2(run);
t1.join();
t2.join(); clock_t end = clock();
cout << "num=" << num << ",spend time:" << end - start << "ms" << endl; return a.exec();
}

运行结果:num=2,spend time:5ms

原子变量。

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; const int N = ;
atomic_int num {}; // 不会发生线程冲突,线程安全 void run()
{
for (int i = ; i < N; ++i){
num++;
}
} int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); clock_t start = clock(); thread t1(run);
thread t2(run);
t1.join();
t2.join(); clock_t end = clock();
cout << "num=" << num << ",spend time:" << end - start << "ms" << endl; return a.exec();
}

C++11 并发之std::atomic。

lambda与多线程

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); auto fun = [](const char* str){cout << str << endl;};
thread t1(fun,"hello world");
thread t2(fun,"hello C++"); return a.exec();
}

时间等待相关

#include <QCoreApplication>
#include<thread>
#include<chrono>
#include <iostream>
#include <mutex>
using namespace std; int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); auto fun = [](const char* str){
this_thread::sleep_for(chrono::seconds());
this_thread::yield();// 让cpu执行其他空闲线程
cout << this_thread::get_id() << endl;
cout << str << endl;
};
thread t1(fun,"hello world"); return a.exec();
}

c++11并发之std::thread的更多相关文章

  1. C++11并发之std::thread<转>

    最近技术上没什么大的收获,也是悲催的路过~ 搞一点新东西压压惊吧! C++11并发之std::thread 知识链接: C++11 并发之std::mutex C++11 并发之std::atomic ...

  2. C++11 并发之std::thread std::mutex

    https://www.cnblogs.com/whlook/p/6573659.html (https://www.cnblogs.com/lidabo/p/7852033.html) C++:线程 ...

  3. C++11并发之std::mutex

    知识链接: C++11并发之std::thread   本文概要: 1. 头文件. 2.std::mutex. 3.std::recursive_mutex. 4.std::time_mutex. 5 ...

  4. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

  5. C++11并发——多线程std::thread (一)

    https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...

  6. c++11中关于std::thread的join的思考

    c++中关于std::thread的join的思考 std::thread是c++11新引入的线程标准库,通过其可以方便的编写与平台无关的多线程程序,虽然对比针对平台来定制化多线程库会使性能达到最大, ...

  7. c++11中关于`std::thread`线程传参的思考

    关于std::thread线程传参的思考 最重要要记住的一点是:参数要拷贝到线程独立内存中,不管是普通类型.还是引用类型. 对于传递参数是引用类型,需要注意: 1.当指向动态变量的指针(char *) ...

  8. C++ 11 笔记 (五) : std::thread

    这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...

  9. Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)

    昨天练车时有一MM与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...

随机推荐

  1. SSH框架开发蛋糕房管理系统之质量属性

    SSH框架开发蛋糕房管理系统之质量属性 我要开发的系统是基于ssh框架的蛋糕房管理系统.本系统前台提供的主要功能是在线预定蛋糕,本店管理员拥有最高权限,包括收银管理,设备管理,日常销售管理,蛋糕定制管 ...

  2. MyBatis中if,where,set标签

    <if>标签 <select id="findActiveBlogWithTitleLike" resultType="Blog"> S ...

  3. 后端返回值以json的格式返回,前端以json格式接收

    以随便一个类为例子:这个例子是查询企业主营类别前5事项 一.以json数组的格式返回到前端中 (1)后端将结果绑定到param中,然后将结果以为json数组的格式返回到前端 /** * 查询企业主营类 ...

  4. 简话h5唤起本地app

    在没接触这个功能之前,查询各种文档后也只是似懂非懂,做过之后,发现其实很简单,简言之就是通过一个iframe或者a标签来跳转app端提供的URL schema(至于这个URL schema的组成格式, ...

  5. PHP使用Redis实现消息队列

    消息队列可以使用MySQL来实现,可以参考博客PHP使用MySQL实现消息队列,虽然用MySQL可以实现,但是一般不这么用,因为MySQL的数据都存在硬盘中,而从硬盘中对MySQL的操作,I/O花费的 ...

  6. JavaScript中的cookie

    cookie本身没什么可介绍的,但是cookie在JavaScript中,有很多需要注意的 首先,cookie在JavaScript中,是window.document对象的一个属性,所以访问cook ...

  7. java mail smtp port

    https://www.tutorialspoint.com/javamail_api/javamail_api_smtp_servers.htm https://www.mkyong.com/jav ...

  8. Activiti中子流程:SubProcess,CallActiviti的区别

    子流程:SubProcess,CallActiviti的区别 https://community.alfresco.com/thread/221771-call-activiti-vs-subproc ...

  9. [日常工作] cmd以及bash 直接使用当前目录的方法

    1. 从知乎学到了一点.. 2. 之前想在比如f:\a\b 目录下执行cmd命令的时候 总是需要先 f: 再cd目录的方式. 3. 知乎上面学到 发现可以通过在当前目录下面 输入  cmd 或者是 b ...

  10. Android提供的layout文件存放位置

    在编程的过程中,会用到android.R.layout下的一些常量.与这些常量对应的,Android提供了对应点的layout布局文件. android.jar中有对应的xml文件,但是打开的时候通常 ...