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的更多相关文章

  1. 采用std::thread 替换 openmp

    内容转换的,具体详见博客:https://cloud.tencent.com/developer/article/1094617 及对应的code:https://github.com/cpuimag ...

  2. 【boost】使用装饰者模式改造boost::thread_group

    在项目中使用boost::thread_group的时候遇到几个问题: 1.thread_group不提供删除全部thread列表的方法,一直使用create会是其内部列表不断增加. 2.thread ...

  3. C++ 11 笔记 (五) : std::thread

    这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...

  4. std::thread使用

    本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...

  5. Microsoft C++ 异常: std::system_error std::thread

    第一次使用std::thread,把之前项目里面的Windows的thread进行了替换,程序退出的然后发生了std::system_error. 经过调试,发现std::thread ,join了两 ...

  6. std::thread(2)

    个线程都有一个唯一的 ID 以识别不同的线程,std:thread 类有一个 get_id() 方法返回对应线程的唯一编号,你可以通过 std::this_thread 来访问当前线程实例,下面的例子 ...

  7. 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 ...

  8. std::thread “terminate called without an active exception”

    最近在使用std::thread的时候,遇到这样一个问题: std::thread t(func); 如果不使用调用t.join()就会遇到 "terminate called whitho ...

  9. C++17 std::shared_mutex的替代方案boost::shared_mutex

    C++17 std::shared_mutex的替代方案boost::shared_mutex C++17boost  std::shared_mutex http://en.cppreference ...

随机推荐

  1. 第四十二篇、自定义Log打印

    1.在Xcode 8出来之后,需要我们去关闭多余的日志信息打印 2.在开发的过程中,打印调试日志是一项比不可少的工程,但是在iOS 10中NSLog打印日志被屏蔽了,就不得不使用自定义Log 3.去掉 ...

  2. iOS - 字典(NSDictionary)

    1. 字典类型的常用处理 //---------------不可变字典 //1.字典的创建 NSArray *array1 = [NSArray arrayWithObjects:@"zha ...

  3. iOS开发——model类模板(过滤null和ID)

            说明:model类模板已默认过滤null值,附加特殊情况的关键字ID名的冲突(需手动去掉注释代码).MyMessageModel为示例的名字.可以自己随便起. 1.自己创建一个继承与N ...

  4. 一些常用sqlite语句

    1,如果表不存在就新建一个 CComBSTR bstrCreatBat(L”CREATE TABLE IF NOT EXISTS tb_Name (\ rowIdIndex  INTEGER PRIM ...

  5. Xamarin 实现android gridview 多选

    参考文章:http://blog.csdn.net/zhouyuanjing/article/details/8372686 GridView初始化代码: gridViewStudent = Find ...

  6. springMVC整合memcached

    非原创,文章转自:http://www.cnblogs.com/xiaoqingxin/p/4132391.html 文章我就不全copy了,摘抄下我关注的部分,想看原文的请移步上面文章链接 appl ...

  7. StringBuilder的Append()方法会比+=效率高

    StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id from " + databa ...

  8. C# 获取中文星期的两种方法

    //方法一 public string Week() { string[] weekdays = { "星期日", "星期一", "星期二" ...

  9. SpecFlow教程--快速入门

    原文http://www.specflow.org/getting-started/ 一.安装 为了能正确安装SpecFlow所需要的东西,你必须安装集成IDE的插件以及设置你的项目使用SpecFlo ...

  10. ACE_linux:任务 & 命令(Task and Command)

    1.涉及类 ACE_Task//ACE任务ACE_Activation_Queue//ACE命令队列 ACE_Method_Request//ACE请求(命令) 2.简介 ACE主动对象模式 主动对象 ...