std::thread
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的更多相关文章
- 用std::thread替换实现boost::thread_group
thread_group是boost库中的线程池类,内部使用的是boost::thread. 随着C++ 11标准的制定和各大编译器的新版本的推出(其实主要是VS2012的推出啦……),本着能用标准库 ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- Cocos2dx 3.0 过渡篇(二十六)C++11多线程std::thread的简单使用(上)
昨天练车时有一MM与我交替着练,聊了几句话就多了起来,我对她说:"看到前面那俩教练没?老色鬼两枚!整天调戏女学员."她说:"还好啦,这毕竟是他们的乐趣所在,你不认为教练每 ...
- C++11多线程std::thread的简单使用
在cocos2dx 2.0时代,我们使用的是pthread库,是一套用户级线程库,被广泛地使用在跨平台应用上.但在cocos2dx 3.0中并未发现有pthread的支持文件,原来c++11中已经拥有 ...
- Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的
下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...
- std::thread使用
本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...
- linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决
linux每日一练:Enable multithreading to use std::thread: Operation not permitted问题解决 在linux在需要使用c++11时会遇到 ...
- Microsoft C++ 异常: std::system_error std::thread
第一次使用std::thread,把之前项目里面的Windows的thread进行了替换,程序退出的然后发生了std::system_error. 经过调试,发现std::thread ,join了两 ...
随机推荐
- [6]Telerik TreeView 复选框
参考连接:http://demos.telerik.com/aspnet-mvc/razor/treeview/clientsideapi 问题: Telerik TreeView 选择或取消 父选项 ...
- PPPOE原理及部署
PPPOE 1,一个广播域 2,panabit可以做小区项目 http://edu.51cto.com/course/course_id-3849.html Adsl的介绍 所谓非对称,即上下行速 ...
- chrome扩展
chrome拓展开发实战:页面脚本的拦截注入 时间 2015-07-24 11:15:00 博客园精华区 原文 http://www.cnblogs.com/horve/p/4672890.htm ...
- 实践:VIM深入研究(20135301 && 20135337)
目录 一.基本知识 1.vim模式介绍 2.三种常用模式的切换 二.Vim文档编辑 1.vim重复命令 2.游标的快速跳转 3.复制粘贴和剪切 4.删除文本 5.字符的替换及撤销(Undo操作) 6. ...
- GDB深入研究——20135308芦畅
GDB深入研究 一.GDB代码调试 (一)GDB调试实例 在终端中编译一个示例C语言小程序,保存到文件 gdb-sample.c 中,用GCC编译之 #include <stdio.h> ...
- iOS中UIMenuController的使用
不知你有没有发现,在你的微信朋友中,长按一段文字(正文或者评论),会弹出这样子的玩意: 要想在你的view或者viewController中实现长按弹出菜单栏你必须要调用becomeFirstResp ...
- python中的模块
用python其实好久了,但是一直没有特别系统的学习过,当年迅速上手,也写了好多代码了,零零散散也学了很多知识点.到了把它们串起来的时候了.尝试记录一下系统整理的知识点,先从“模块”说起. 为什么需要 ...
- MVC5 + EF6 + Bootstrap3 (12) 新建数据
Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-create.html 系列教程:MVC5 + EF6 + ...
- 收藏所用C#技术类面试、笔试题汇总
技术类面试.笔试题汇总 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补 ...
- 需要中文版《The Scheme Programming Language》的朋友可以在此留言(内附一小段译文)
首先给出原著的链接:http://www.scheme.com/tspl4/. 我正在持续翻译这本书,大概每天都会翻译两小时.若我个人拿不准的地方,我会附上原文,防止误导:还有些不适合翻译的术语,我会 ...