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. Eclipse添加注释简介

    (1)在方法或者属性上面添加注释:在方法或者属性字段的上面一行输/**,然后回车.一般情况下添加的注释格式如下所示,当然注释的格式是可以修改的:   1 2 3 4 5 /**   * @param ...

  2. iframe在ipad safari的显示

    今 天要在web中嵌套一个网址或本地HTML,用到了iframe,在电脑上设置scrolling=‘auto’,宽度高度,会有滚动条出现.而在 ipad上会全部显示整个网页的宽度高度.scrollin ...

  3. OpenCV人脸检测demo--facedetect

    &1 问题来源 在运行官网的facedetect这个demo的时候,总是不会出来result的图形,电脑右下角提示的错误是“显示器驱动程序已停止响应,而且已恢复 windows 8(R)”. ...

  4. iOS 苹果自带地图定位Core Location

    Core Location是iOS SDK中一个提供设备位置的框架.可以使用三种技术来获取位置:GPS.蜂窝或WiFi.在这些技术中,GPS最为精准,如果有GPS硬件,Core Location将优先 ...

  5. 基于win32的socket编程及程序实现

    初步研究了win32平台的Windows Sockets,它是Microsoft Windows的网络程序设计接口,它是从Berkeley Sockets扩展而来的,以动态链接库的形式提供给我们使用. ...

  6. android的progressDialog 的使用。android数据异步加载 对话框提示

    在调用的Activity中定义一个全局的 progressDialog 点击按钮的时候调用下面这句 progressDialog = ProgressDialog.show(SearchActivit ...

  7. C# GC 垃圾回收机制

    今天来谈谈C# 的GC ,也就是垃圾回收机制,非常的受教,总结如下 首先:谈谈托管,什么叫托管,我的理解就是托付C# 运行环境帮我们去管理,在这个运行环境中可以帮助我们开辟内存和释放内存,开辟内存一般 ...

  8. java中的自增问题

    运行下面这段代码,其结果是什么呢? package com.test; public class Inc { public static void main(String[] args) { Inc ...

  9. 19.C#逐一介绍IEnumerable和IEnumerable<T>中的扩展方法(10.3-10.5)

    今天没有太多的言语,只有代码,扩展方法多得太多,不能一一列完,书中一些,看多了也就会使用了. //Enumerable.Range 返回起始到结束范围,是一个Enumrable<int>类 ...

  10. 第二课:判断js变量的类型以及domReady的原理

    1.类型的判断: js五种简单数据类型有:null,undefined,boolean,number,string. 还有复杂的数据类型:Object,Function,RegExp,Date,自定义 ...