用std::thread替换实现boost::thread_group
thread_group是boost库中的线程池类,内部使用的是boost::thread。
随着C++ 11标准的制定和各大编译器的新版本的推出(其实主要是VS2012的推出啦……),本着能用标准库就用标准库的指导原则,决定把项目中多线程相关的部分代码从boost::thread迁移到std::thread。
thread的迁移本身很简单,毕竟stl的很多功能是直接从boost发展而来的,基本上就是改一下头文件和名称空间的问题,例外是thread_group,thread_group是boost的组件,但并不是标准库的组件,所以需要自己实现一下。
说是自己实现,其实就是复制粘贴boost中的代码啦。但boost中的thread_group使用shared_mutex来进行线程同步,shared_mutex也没有进入标准库,所以需要用什么东西来替代一下。shared_mutex没能进入标准库的原因就是C++标准化委员会认为目前可行的shared_mutex实现方案在性能上并不合算,不如直接使用mutex,既然如此,就直接用mutex吧。相应的shared_lock也修改成lock_guard,完成。

#include <thread>
#include <mutex>
#include <list>
#include <memory>
namespace std
{
//兼容boost::thread_group
//使用std::thread代替boost::thread,std::mutex代替boost::shared_mutex
class thread_group
{
private:
thread_group(thread_group const&);
thread_group& operator=(thread_group const&);
public:
thread_group() {}
~thread_group()
{
for(auto it=threads.begin(),end=threads.end(); it!=end;++it)
{
delete *it;
}
} template<typename F>
thread* create_thread(F threadfunc)
{
lock_guard<mutex> guard(m);
auto_ptr<thread> new_thread(new thread(threadfunc));
threads.push_back(new_thread.get());
return new_thread.release();
} void add_thread(thread* thrd)
{
if(thrd)
{
lock_guard<mutex> guard(m);
threads.push_back(thrd);
}
} void remove_thread(thread* thrd)
{
lock_guard<mutex> guard(m);
auto it=std::find(threads.begin(),threads.end(),thrd);
if(it!=threads.end())
{
threads.erase(it);
}
} void join_all()
{
lock_guard<mutex> guard(m);
for(auto it=threads.begin(),end=threads.end();it!=end;++it)
{
(*it)->join();
}
} size_t size() const
{
lock_guard<mutex> guard(m);
return threads.size();
} private:
list<thread*> threads;
mutable mutex m;
};
}

——其实完全没意义嘛……
用std::thread替换实现boost::thread_group的更多相关文章
- 采用std::thread 替换 openmp
内容转换的,具体详见博客:https://cloud.tencent.com/developer/article/1094617 及对应的code:https://github.com/cpuimag ...
- 【boost】使用装饰者模式改造boost::thread_group
在项目中使用boost::thread_group的时候遇到几个问题: 1.thread_group不提供删除全部thread列表的方法,一直使用create会是其内部列表不断增加. 2.thread ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- std::thread使用
本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...
- Microsoft C++ 异常: std::system_error std::thread
第一次使用std::thread,把之前项目里面的Windows的thread进行了替换,程序退出的然后发生了std::system_error. 经过调试,发现std::thread ,join了两 ...
- std::thread(2)
个线程都有一个唯一的 ID 以识别不同的线程,std:thread 类有一个 get_id() 方法返回对应线程的唯一编号,你可以通过 std::this_thread 来访问当前线程实例,下面的例子 ...
- Enable multithreading to use std::thread: Operation not permitted问题解决
在用g++ 4.8.2编译C++11的线程代码后,运行时遇到了如下报错: terminate called after throwing an instance of 'std::system_err ...
- std::thread “terminate called without an active exception”
最近在使用std::thread的时候,遇到这样一个问题: std::thread t(func); 如果不使用调用t.join()就会遇到 "terminate called whitho ...
- C++17 std::shared_mutex的替代方案boost::shared_mutex
C++17 std::shared_mutex的替代方案boost::shared_mutex C++17boost std::shared_mutex http://en.cppreference ...
随机推荐
- sqlserver 测试sql语句执行时间
查看sql语句执行时间/测试sql语句性能 写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置S ...
- 【转】C# 后台开启 cmd执行命令
private void RunCmd(string cmd) { System.Diagnostics.Process p = new System.Diagnostics. ...
- 32位系统下使用4GB内存
64位系统的驱动还有不少缺陷,果断重装回32位系统,但是4gb的内存,明显是浪费啊. 所以必须利用起来. 我没有采用不稳定的破解内核的做法,采用了虚拟硬盘的做法.因为个人觉得这样其实利用效率更高. 方 ...
- javascript笔记——placehold
<input type="text" name="搜索" value="搜索" placeholder="搜索" ...
- 【转】揭开Socket编程的面纱
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)
e). 消息延迟发送(和前面没太大区别直接上代码) #include <iostream> #include "caf/all.hpp" #include " ...
- InnoDB 离线转储工具
一,应用场景; 1,表空间严重损坏,无法恢复;2,数据库表空间文件丢失后从磁盘上打捞出部分数据页面;3,恢复删除记录; 二,功能; 从数据页中直接转储出文本格式的行数据,从而可以后期用 LOAD DA ...
- shell命令行快速编辑命令
ctrl r:命令行出现 reverse-i-search,输入字符将在输入历史中匹配命令 ctrl p:向前翻看历史 ctrl n:向后翻看历史 ctrl a:命令行首 ctrl e:命令行尾 ct ...
- 第八章 Qt GUI之对话框使用
第八章 Qt GUI之对话框使用 对话框可以是模态(modal)的或非模态(modeless)两种.当我们在一个用户界面程序里面对一个对话框(比如选择文件对话框)的操作没有结束前,界面的其他窗口无法操 ...
- Jquery get parameter value
http://www.sitepoint.com/url-parameters-jquery/ $.urlParam('id') ==> $.urlParam = function(name){ ...