std::shared_ptr<std::thread> m_spThread;

m_spThread.reset(new std::thread(std::bind(&GameServer::process_thread, this)));

void GameServer::process_thread()
{
try
{
process_thread_try();
}
catch (...)
{
DWORD e = GetLastError();
int a = ;
}
}
std::bind(&GameServer::process_thread, this)返回一个std::function,绑定成员函数process_thread,然后new std::thread(std::Funciton)返回thread*
reset源码这样的
template<class _Ux>
void reset(_Ux *_Px)
{ // release, take ownership of _Px
shared_ptr(_Px).swap(*this);
}

就是交换智能指针管理的对象,m_spThread管理这个对象,之后线程函数就自动执行了,我有个疑问为什么这个不用Join或者detach?

具体看下:

一个 std::thread 对象可以接收

普通的函数
函数对象
类的成员函数
lambda 函数
作为参数。 #include <QtCore>
#include <thread>
普通的函数
void test1()
{
qDebug()<<"hello test 1";
} void test2(const QString &text)
{
qDebug()<<"hello"<<text;
}
函数对象
class Test3
{
public:
Test3(){}
void operator()() const
{
qDebug()<<"hello test3"<<this;
}
}; class Test4
{
public:
QString a;
Test4(const QString a):a(a){}
void operator()(const QString b) const
{
qDebug()<<a<<b;
}
};
类的成员函数
class Test5
{
public:
void output()
{
qDebug("hello test 5");
}
}; class Test6
{
public:
void output(const QString & text)
{
qDebug()<<"hello"<<text;
}
};
以及lambda函数
主程序
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::thread t1(test1);
std::thread t2(test2, "test 2");
std::thread t3((Test3()));
std::thread t4(Test4("hello"), "test 4");
Test5 test5;
std::thread t5(&Test5::output, &test5);
Test6 test6;
std::thread t6(&Test6::output, &test6, "test 6");
std::thread t7([](const QString & text){qDebug()<<"hello"<<text;}, "test7");
return a.exec();
}
备忘
添加了两层括号(不然会被编译器认作一个名为t3的函数的声明) std::thread t3((Test3()));
std::bind ? 带参数,比如: std::thread t2(test2, "test 2");
要写成 std::thread t2(std::bind(test2, "test 2"));
那一个标准? std::ref ? (函数对象,或者参数,都是传值,传引用需要使用std::ref),比如对前面的Test3 Test3 test3;
test3();
std::thread t3(test3);
std::thread t33(std::ref(test3));
这三次调用的结果(类似于): hello test3 0xbfc3b27f
hello test3 0xbfc3b27f
hello test3 0x9811e60

std::thread的更多相关文章

  1. 用std::thread替换实现boost::thread_group

    thread_group是boost库中的线程池类,内部使用的是boost::thread. 随着C++ 11标准的制定和各大编译器的新版本的推出(其实主要是VS2012的推出啦……),本着能用标准库 ...

  2. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

  3. C++ 11 笔记 (五) : std::thread

    这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...

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

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

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

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

  6. Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

  7. std::thread使用

    本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...

  8. linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决

    linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决 在linux在需要使用c++11时会遇到 ...

  9. Microsoft C++ 异常: std::system_error std::thread

    第一次使用std::thread,把之前项目里面的Windows的thread进行了替换,程序退出的然后发生了std::system_error. 经过调试,发现std::thread ,join了两 ...

随机推荐

  1. 16Mybatis_动态sql_if判断

    mybatis的核心就是动态sql. 什么是动态sql:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 这篇文章讲解sql中的if语句.它可以对查询条件进行判断,如果输入参 ...

  2. 15Mybatis_输出类型

    输出类型分为两种:1.resultType          和         2.resultMap 接下来先讲解resultType: 使用resultType进行输出映射,只有查询出来的列名和 ...

  3. android intent隐式调用之一个应用程序启动另一个应用程序

    理解Intent的关键之一是理解清楚Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似:另一种是隐式的Intent,即Inten ...

  4. winform listbox与textbox组合提示框 模糊查询

      private void listbox1_MouseClick(object sender, MouseEventArgs e)        {            textbox1.Vis ...

  5. C语言 原码--反码--补码

    //原码,反码,补码 #include<stdio.h> #include<stdlib.h> //数值的表示方法——原码.反码和补码 //原码:最高位为符号位,其余各位为数值 ...

  6. ZooKeeper学习第八期——ZooKeeper伸缩性

    一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都已经知道了在ZooKeeper集群当中有两种角色Leader和Follower.Leader可以接受 ...

  7. JS 之继承

    ECMAScript继承是通过原型链来继承的.基本思想是利用原型来让一个引用类型继承另一个引用类型的属性和方法,使原型变为另一个对象的实例.通过原型链实现继承时,不能使用对象字面量创建原型方法,避免重 ...

  8. [bzoj 1026]windy数(数位DP)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 分析: 简单的数位DP啦 f[i][j]表示数字有i位,最高位的数值为j的windy数总 ...

  9. 求根号m(巴比伦算法)

    巴比伦算法是针对求根号m的近似值情况的,它的思想是这样的: 设根号m=X0,则如果枚举有答案X(X<X0),则m/X>X0,当精度要求不高的时候,我们可以看成X=m/X=X0,而如果精度要 ...

  10. [C#]exchange发送,收件箱操作类

    最近项目中需要用到exchange的操作,就参照msdn弄了一个简单的操作类.目前先实现了,发送邮件和拉取收件箱的功能,其他的以后在慢慢的添加. using Microsoft.Exchange.We ...