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. 谷歌验证 (Google Authenticator) 的实现原理是什么?

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:徐小花链接:http://www.zhihu.com/question/20462696/answer/18731073来源: ...

  2. Linux 结束进程

    一个进程由于以下5个原因中的一个终止 --main函数调用return; --调用exit函数--C语言库函数: --调用_exit函数--系统调用 --调用abort函数 --被一个信号终止.(ki ...

  3. SpringMVC中的@PathVariable

    @PathVariable是用来动态获得url中的参数的,代码示例如下: 可以在代码中获得lev_1.lev_2和target参数的值看一下 // 支持跳转到WEB-INF/目录下二层目录 @Requ ...

  4. SQL SERVER with递归示例一则

    WITH SUBQUERY AS ( SELECT ORGID FROM OM_ORGANIZATION WHERE PARENTORGID = 'ROOT' UNION ALL SELECT B.O ...

  5. ios 定位 监听是否跨入某个指定的区域

    /*****监听用户是否进入和走出 在某个区域*****/ 1 #import "ViewController.h" #import <CoreLocation/CoreLo ...

  6. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  7. IOS开发之——意见反馈UITextView的使用+不能输入字符输入

    @interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...

  8. 8.HBase In Action 第一章-HBase简介(1.2.2 捕获增量数据)

    Data often trickles in and is added to an existing data store for further usage, such as analytics, ...

  9. wpf键盘记录器

    很简单的一个wpf键盘记录器 这个程序我一样用了全局勾子,之前用的都是winform上运行了,前一段时间 在国外的论坛上逛看到了一个wpf能用的就做了一个小程序记录一下,为了方便大家直关的看我在页面上 ...

  10. memcached 适用的场景

    最近在看 memcached  的公共课,发现memcache的确是个好东西,可以显著地减小数据库负载,当然我们要搞清楚,任何一样技术都有它的优缺点, 在使用它的时候,搞清楚它的适用场景,才能扬长避短 ...