知识链接:

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

最新文章

  1. css 妙味 总结
  2. C++ Primer : 第十三章 : 拷贝控制示例
  3. NBUT 1028 该减肥了(简单递推)
  4. Memcached学习(一)
  5. 5.MVC框架开发(强类型开发,控制器向界面传递数据的几种方法)
  6. 译文:如何使用SocketAsyncEventArgs类(How to use the SocketAsyncEventArgs class)
  7. php7 install memcached extension
  8. 隐藏Nginx版本号的安全性与方法
  9. 《C#语言和数据库技术基础》单词必备
  10. Https握手协议以及证书认证
  11. 使用eclipse写C
  12. mac 上传本地代码到 Github 教程
  13. jpa @onetomany 级联查询时会有重复数据,去重问题
  14. RT-SA-2019-003 Cisco RV320 Unauthenticated Configuration Export
  15. MySQL实用基本操作
  16. Python学习(七) —— 装饰器、迭代器、生成器
  17. 企业应用打包的时候 修改ipa包的bundle identifier
  18. Fragment的坑
  19. iptables之四表五链
  20. hdu4467 Graph

热门文章

  1. Centos6.9下RocketMQ3.4.6高可用集群部署记录(双主双从+Nameserver+Console)
  2. Gitblit版本服务器环境部署记录
  3. [Beta]M2事后分析
  4. PAT 1022 D进制的A+B
  5. BFC——块级格式化上下文
  6. Tomcat7/8访问Server Status、Manager App、Host Manager出现403 forbidden
  7. Java之修改文件内容:字符串逐行替换
  8. 【转】CNN卷积神经网络_ GoogLeNet 之 Inception(V1-V4)
  9. Nginx referer防盗链模块
  10. 一点点linux系统的学习心得