用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 ...
随机推荐
- 很牛逼的android真机调试,手机、平板、电视都可
1.首先通过路由器,搭建局域网 2.手机开wifi,记住ip地址,平板开wifi,记住ip地址,电视开wifi,记住ip 3.然后再eclipse里面“窗口-首选项-android-ddms里面设置使 ...
- jquery的map()和each()方法
1. map()方法 //找到所有的标题元素,映射它们的ID,并转化为数组后排序 $(':header').map(function(){return this.id}).toArray().sort ...
- ATL一:CWindowImpl
1,#ifdef _ATL_DISABLE_NO_VTABLE#define ATL_NO_VTABLE#else#define ATL_NO_VTABLE __declspec(novtable)# ...
- Ubuntu gedit 折叠插件
Ubuntu Kylin 14.04 gedit - Version 3.10.4 (as same as all version of gedit 3.x ) Attention: this pl ...
- SSH连接时出现Host key verification failed的原因及解决方法
SSH连接的时候Host key verification failed. [root@cache001 swftools-0.9.0]# ssh 192.168.1.90@@@@@@@@@@@@@@ ...
- 第七章 探秘Qt的核心机制-信号与槽
第七章 探秘Qt的核心机制-信号与槽 注:要想使用Qt的核心机制信号与槽,就必须在类的私有数据区声明Q_OBJECT宏,然后会有moc编译器负责读取这个宏进行代码转化,从而使Qt这个特有的机制得到使用 ...
- apache重写
---- 本文旨在提供如何用Apache重写规则来解决一些常见的URL重写方法的问题,通过常见的实例给用户一些使用重写规则的基本方法和线索. 一.为什么需要用重写规则 ---- 网站的生命在于不断地进 ...
- mysql table readonly
if the table does not have primary key or unique non-nullable defined, then MySql workbench could no ...
- 指针之 *((volatile unsigned long *)(x))解析
今天重新温习了一下C语言的指针部分,突然想到了经常会碰见的一种宏定义:#define PGAS (*((volatile unsinged long *)(x))) 在解析该宏定义前,先看看指针变量的 ...
- PHP实现中文简体字和繁体字互转
function convert($str, $action='S'){ if($action != 'S' && $action != 'T'){ return $str; } $s ...