//#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创建方式的更多相关文章

  1. C++11多线程std::thread的简单使用

    在cocos2dx 2.0时代,我们使用的是pthread库,是一套用户级线程库,被广泛地使用在跨平台应用上.但在cocos2dx 3.0中并未发现有pthread的支持文件,原来c++11中已经拥有 ...

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

    本篇接上篇继续讲:上篇传送门:http://blog.csdn.net/star530/article/details/24186783 简单的东西我都说的几乎相同了,想挖点深的差点把自己给填进去. ...

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

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

  4. C++11多线程编程--线程创建

    参考资料 adam1q84 我是一只C++小小鸟 Thread support library Book:<C++ Concurrency in Action> 线程的创建 线程的创建有多 ...

  5. Java多线程——线程的创建方式

    Java多线程——线程的创建方式 摘要:本文主要学习了线程的创建方式,线程的常用属性和方法,以及线程的几个基本状态. 部分内容来自以下博客: https://www.cnblogs.com/dolph ...

  6. 【C++11应用】基于C++11及std::thread实现的线程池

    目录 基于C++11及std::thread实现的线程池 基于C++11及std::thread实现的线程池 线程池源码: #pragma once #include <functional&g ...

  7. Java:多线程概述与创建方式

    目录 Java:多线程概述与创建方式 进程和线程 并发与并行 多线程的优势 线程的创建和启动 继承Thread类 start()和run() 实现Runnable接口 实现Callable接口 创建方 ...

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

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

  9. C++11并发编程:多线程std::thread

    一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...

随机推荐

  1. 测试库异常down分析(abnormal instance termination)

    客户测试库,down问题分析,根据alert 的问题指向,实例异常终止,但是无其它有价值的信息 Terminating the Instance Due to Error Out-Of-Memory( ...

  2. 转 实现类似QQ的窗体停靠

    [DllImport("User32.dll")] public static extern bool PtInRect(ref Rectangle Rects, Point lp ...

  3. POJ1322Chocolate--概论DP

    题目在这里 每次从包装中取出一块巧克力并放在桌子上.如果桌子上有两个相同颜色的巧克力,则将这两个丢掉.如果包中有C种颜色的巧克力(颜色均匀分布),从包装中取出N个巧克力后,桌子上确实有M个巧克力的概率 ...

  4. 【转载】Response对象的作用以及常用方法属性

    Response对象是Asp.Net应用程序中非常重要的一个内置对象,其作用为负责将服务器执行好的信息输出给客户端,即作用主要为响应客户端请求并将服务器的响应返回给用户,在页面的临时跳转中,也可使用R ...

  5. 让image居中对齐,网页自适应

    <div class="page4_content"> <div class="page4_box"> <div class=&q ...

  6. 原生js实现ajax封装

    一.什么是ajax? 定义:Ajax(Asynchronous Java and XML的缩写)是一种异步请求数据的web开发技术,在不需要重新刷新页面的情况下,Ajax 通过异步请求加载后台数据,并 ...

  7. ORA-00001:unique constraint violated解决

    转自:https://www.2cto.com/database/201211/172340.html ORA-00001:unique constraint violated解决 今天往数据库中插入 ...

  8. MongoDB的基础概念

    1.MongoDB和传统数据库的概念区别 database       database        数据库table           collection    数据库表/集合row      ...

  9. Centos 6.5 Apache服务安装

     Apache是什么: Apache HTTP Server(简称Apache)是Apache软件基金会的一个高性能.功能强大.稳定可靠.又很灵活的开发源代码的web服务软件,对linux的支持相对好 ...

  10. tcping端口检测工具使用

    大家都知道检测网络状态是,无论是服务器/客户机 最常用的就是ping命令,但ping命令只能检测ICMP协议,若对方禁止ping协议了,自然ping命令也就无法检测了,此时,我们可以通过tcping工 ...