用std::thread替换实现boost::thread_group

简介: thread_group是boost库中的线程池类,内部使用的是boost::thread。 随着C++ 11标准的制定和各大编译器的新版本的推出(其实主要是VS2012的推出啦……),本着能用标准库就用标准库的指导原则,决定把项目中多线程相关的部分代码从boost::thread迁移到std::thread。

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; }; }
复制代码

——其实完全没意义嘛……

目录
相关文章
|
27天前
|
存储 前端开发 算法
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析(一)
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析
42 0
|
27天前
|
存储 并行计算 Java
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析(二)
C++线程 并发编程:std::thread、std::sync与std::packaged_task深度解析
61 0
|
27天前
|
算法 API 调度
深入探索:在std::thread中创建并管理QEventLoop的全面指南(一)
深入探索:在std::thread中创建并管理QEventLoop的全面指南
35 1
|
27天前
|
存储 安全 调度
深入探索:在std::thread中创建并管理QEventLoop的全面指南(二)
深入探索:在std::thread中创建并管理QEventLoop的全面指南
25 2
|
3月前
|
安全 C++
C++ std::thread::detch函数之遇坑记录
调用thread::detch后,程序有可能会在当前调用函数执行完之后才去构造实参对象
16 0
C++ std::thread::detch函数之遇坑记录
|
7月前
std::jthread与std::thread区别
std::jthread是C++20新引入的线程类,与 std::thread 类似,或者说,jthread是对thread进一步的封装,功能更强大。
|
10月前
|
存储 C++
C++11 thread_local的 用法(二)
C++11 thread_local的 用法(二)
88 0
|
10月前
|
编译器 程序员 C语言
【玩转RT-Thread】RT-Thread内核宏定义详解(rtdef.h)
【玩转RT-Thread】RT-Thread内核宏定义详解(rtdef.h)
425 0
error: ‘to_string‘ is not a member of ‘std‘ 或 error: ‘thread‘ is not a member of ‘std‘ 原因及解决办法
error: ‘to_string‘ is not a member of ‘std‘ 或 error: ‘thread‘ is not a member of ‘std‘ 原因及解决办法
1024 0
|
Java 调度 API
java之Thread.sleep(long)与object.wait()/object.wait(long)的区别及相关概念梳理(good)
一、Thread.sleep(long)与object.wait()/object.wait(long)的区别sleep(long)与wait()/wait(long)行为上有些类似,主要区别如下:1.Thread.sleep(long)是属于Thread类的静态方法。
1334 0