C++11多线程std::thread创建方式
//#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 线程传参实例应用
C++11多线程std::thread创建方式的更多相关文章
- C++11多线程std::thread的简单使用
在cocos2dx 2.0时代,我们使用的是pthread库,是一套用户级线程库,被广泛地使用在跨平台应用上.但在cocos2dx 3.0中并未发现有pthread的支持文件,原来c++11中已经拥有 ...
- Cocos2dx 3.0 过渡篇(二十七)C++11多线程std::thread的简单使用(下)
本篇接上篇继续讲:上篇传送门:http://blog.csdn.net/star530/article/details/24186783 简单的东西我都说的几乎相同了,想挖点深的差点把自己给填进去. ...
- Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)
昨天练车时有一MM与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...
- C++11多线程编程--线程创建
参考资料 adam1q84 我是一只C++小小鸟 Thread support library Book:<C++ Concurrency in Action> 线程的创建 线程的创建有多 ...
- Java多线程——线程的创建方式
Java多线程——线程的创建方式 摘要:本文主要学习了线程的创建方式,线程的常用属性和方法,以及线程的几个基本状态. 部分内容来自以下博客: https://www.cnblogs.com/dolph ...
- 【C++11应用】基于C++11及std::thread实现的线程池
目录 基于C++11及std::thread实现的线程池 基于C++11及std::thread实现的线程池 线程池源码: #pragma once #include <functional&g ...
- Java:多线程概述与创建方式
目录 Java:多线程概述与创建方式 进程和线程 并发与并行 多线程的优势 线程的创建和启动 继承Thread类 start()和run() 实现Runnable接口 实现Callable接口 创建方 ...
- C++11并发——多线程std::thread (一)
https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...
- C++11并发编程:多线程std::thread
一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...
随机推荐
- Javascritp Array数组方法总结
合并数组 - concat() 用法一 (合并两个数组) var hege = ["Cecilie", "Lone"]; var stale = [" ...
- metasploit安卓木马
metasploit---安卓木马入侵 (仅供学习使用,禁止非法使用) 1.生成木马程序 msfvenom -p android/meterpreter/reverse_tcp LHOST=本机ip ...
- js中0.1+0.2 与0.3的对比
Math.abs(0.1+0.2-0.3)<=Number.EPSILON
- 安卓开发之生成cache目录和files目录
package com.lidaochen.test; import android.os.Bundle; import android.support.v7.app.AppCompatActivit ...
- ArduPilot简介
源码地址:https://github.com/ArduPilot/ardupilot/ 参考:http://ardupilot.org/dev/docs/learning-the-ardupilot ...
- CoAP协议
CoAP(Constrained Application Protocol) CoAP是6LowPAN协议栈中的应用层协议 CoAP是超轻量型协议 CoAP的默认UDP端口号为5683 1. 四种消息 ...
- cocos-js 精灵移动转圈
cc.Class({ extends: cc.Component, properties: { carModel: { default: null, type: cc.Sprite }, bgMode ...
- MySQL常见问题集锦及注意事项
一.表设计上的坑 1.字段设计 1.1 字段类型设计 尽量使用整型表示字符串: `INET_ATON(str)`,address to number `INET_NTOA(number)`,numbe ...
- 修改ssh的22端口
将ssh22端口修改为12345 https://www.cnblogs.com/chen-lhx/p/3974605.html # iptables开放12345端口iptables -I INPU ...
- dubbo API的使用方式
本文使用maven方式 1:pom文件 <dependencies> <!-- 引入spring的jar --> <dependency> <groupId& ...