知识链接:

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. CrackMe005-下篇 | 逆向破解分析 | 160个CrackMe(视频+图文)深度解析系列

    作者:逆向驿站微信公众号:逆向驿站知乎:逆向驿站 CrackMe005,上篇说了具体方法,下篇来发逆向分析过程,看看老夫是如何得到上篇的具体方法的! 准备 [环境和工具] win7/xp虚拟机环境 C ...

  2. PHP从入门到精通(二)

     PHP从入门到精通 之PHP中的函数 各位开发者朋友大家好,自上次更新PHP的相关知识,得到了大家的广泛支持.PHP的火爆程度不言而喻,函数作为PHP中极为重要的部分,应诸位的支持,博主继续跟进更新 ...

  3. Visual Studio2015安装过程以及单元测试

    安装环境: 安装版本: Visual Studio2015 安装过程: 因为我是在第一次老师安排的作业的时候感觉VC++6.0不如VS方便所以才装的Visual Studio2015,又安装了点插件, ...

  4. 对wordcount单词字母部分的修改

    原始代码: int s; s = ch; switch (s) { case 'a':letter[0]++; break; case 'b':letter[1]++; break; case 'c' ...

  5. PairProject——结对编程

    成员:12061162  王骜 12061225  钟毅恒 一.合作过程中的照片 . 二.结对编程的优缺点 优点: 1)在编程过程中,任何一段代码都不断地复审,同时避免了将写代码的责任抛给一个人的问题 ...

  6. mooc linux学习总结

    通过八周的学习获得了很多知识.       首先,通过网课老师形象生动的讲述和描述一些专业词汇,使我更加深刻的记住并掌握了这些内容:动态的展示堆栈的变化,更容易理解一段汇编代码:分析操作系统的工作,记 ...

  7. linux内核分析第五次实验

    给MenuOS增加time和time-asm命令 上周是从用户态的观点来理解系统调用,这周从内核态出发研究系统调用,通过跟踪调试,首先把上周的两个命令加到MenuOS中: rm menu -rf 强制 ...

  8. 《Linux内核分析》第八周学习小结 进程的切换和系统的一般执行过程

    进程的切换和系统的一般执行过程 一.进程调度的三个时机: 1.中断处理过程(包括时钟中断.I/O中断.系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记 ...

  9. linux 下gcc 编译结构体问题

    最近在linux 学习c语言的编程,发现好多原来在vs 上的在linux 都编译不过去,今天就遇到了一个问题就是结构体的编译的问题, 结构体大概的定义是 struct Node{ int a; int ...

  10. PAT 1033 旧键盘打字

    https://pintia.cn/problem-sets/994805260223102976/problems/994805288530460672 旧键盘上坏了几个键,于是在敲一段文字的时候, ...