//#include <cstdlib>
//#include <cstdio>
//#include <cstring>
#include <string>
#include <iostream>
#include <thread>
using namespace std; #pragma region C++11 thread基本创建方法 #if 1
// 案例一
void my_print()
{
cout << "线程开始执行了!" << " thread ID= " << std::this_thread::get_id() << endl;
//...
//...
cout << "线程结束执行了!" << " thread ID= " << std::this_thread::get_id() << endl;
} // 案例二
class TA
{
public:
TA()
{
cout << "TA构造函数执行" << this << " thread ID= " << std::this_thread::get_id() << endl;
}
~TA()
{
cout << "~TA析构函数执行" << this << " thread ID= " << std::this_thread::get_id() << endl;
} // 案例二
void operator()() // 不带参数 必须重载() 因子线程需运行函数
{
cout << "线程operator开始执行了!" << this << " thread ID= " << std::this_thread::get_id() << endl;
//...
//...
cout << "线程operator结束执行了!" << this << " thread ID= " << std::this_thread::get_id() << endl;
} TA(const TA& ta)
{
cout << "TA拷贝构造函数执行:" << this << " thread ID= " << std::this_thread::get_id() << endl;
} // 案例四
void thread_fun()
{
cout << "thread_fun执行:" << this << " thread ID= " << std::this_thread::get_id() << endl;
}
}; int main()
{
// 案例一 直接用函数当对象
//std::thread thobj(my_print);
////thobj.join();
//bool b = thobj.joinable();
//thobj.detach();
//b = thobj.joinable(); // 案例二 直接用类对象当对象
TA t;
//std::thread thobj2(t);
std::thread thobj2(std::ref(t)); // 注意这两种的区别 std::ref直接引用原对象,少了一次拷贝和析构
thobj2.join(); // 此时子线程拷贝的对象 析构函数会在前台运行
//thobj2.detach(); // 此时子线程拷贝的对象 析构函数会在后台运行 // 案例三 lambda表达式
//auto my_thread = [] {
// cout << "lambda线程开始执行了!" << endl;
// //...
// //...
// cout << "lambda线程结束执行了!" << endl;
//};
//auto my_thread1 = []()->void {
// cout << "lambda线程开始执行了!" << endl;
// //...
// //...
// cout << "lambda线程结束执行了!" << endl;
//}; //std::thread thobj3(my_thread);
//thobj3.join(); //std::thread thobj4([] {
// cout << "lambda线程开始执行了!" << endl;
// //...
// //...
// cout << "lambda线程结束执行了!" << endl;
//}); //thobj4.join();
//案例四 使用类成员函数作为线程函数
//TA t;
//std::thread thobj5(&TA::thread_fun, &t);
//thobj5.join(); printf("hello jadeshu...\n");
//system("pause");
return 0;
}
#endif
#pragma endregion C++11 thread基本创建方法 #pragma region 线程传参实例应用 //void my_print(const int num, const string &buf)
//{
// cout << num << endl;
// cout << buf << endl;
//}
//
//int main()
//{
// //一、传递临时对象作为线程参数
// int var = 20;
// int& my_var = var;
// char my_buf[] = "this is main.cpp";
// std::thread thobj1(my_print,my_var,string(my_buf));
// thobj1.detach();
// //thobj1.join();
//
// printf("hello jadeshu...\n");
// //system("pause");
// return 0;
//}
#pragma endregion 线程传参实例应用

最新文章

  1. 网络第一节——NSURLConnection
  2. 使用gogs,drone搭建自动部署
  3. ThinkPHP框架表单验证
  4. CRM HomePage.aspx
  5. Informatica Powercenter学习笔记
  6. c#中$.ajax的使用
  7. codeforce Codeforces Round #201 (Div. 2)
  8. png24是支持Alpha透明的。。。。。。
  9. php排序之快速排序
  10. nginx代理tomcat后,tomcat获取真实(非proxy,非别名)nginx服务端ip端口的解决方案
  11. Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging
  12. 51nod_1417:天堂里的游戏
  13. Redis的简单使用和介绍
  14. SpringBoot入门Demo
  15. proc文件系统探索 之 根目录下的文件[1]
  16. 预热ASP.NET MVC 的View
  17. 第二篇:服务消费者(RestTemplate+ribbon)
  18. android textview字体加粗 Android studio最新水平居中和垂直居中
  19. 论文列表——text classification
  20. Vue.js使用-组件(上篇)

热门文章

  1. Windows方便得运行jar文件
  2. Yii2.0 手动添加扩展 redis为例
  3. C#数字日期转成中文日期
  4. 实现CodeFirst自动数据迁移无需手动执行命令
  5. 前端开发 Vue -4promise解读2
  6. 在Linux上安装Zookeeper集群
  7. 【转载】C#使用Except方法求取两个List集合的差集数据
  8. 类中变量私有化和调用:__x和getx/setx或者property
  9. RestFramework之注册器、响应器与分页器
  10. 算法笔试过程中的几个输入输出python语句