使用boost::function 实现一个 单例线程
来源:互联网 发布:推荐俄罗斯代购知乎 编辑:程序博客网 时间:2024/06/06 01:12
所谓单例线程: 是指,线程只运行一个实例,
#ifndef __RM_RUN_ONCE__H__#define __RM_RUN_ONCE__H__#include "boost/scoped_ptr.hpp"#include "boost/thread.hpp"template <class T> /* //NOTE: here only accept boost::function<> type*/class RunOnce {public:RunOnce(T f):_func(f){//_thread.reset(new boost::thread(_func));};RunOnce(){};/*** set func obj*/void set_func(T f){_func = f;}/*** try to run function, ** if the function is already running or completed, just skip over;*/int start(){boost::lock_guard<boost::mutex> lock(_mutex);if(!_thread || ! _thread->joinable()){_thread.reset(new boost::thread(_func));}return 0;};/*** first kill running case(if has),** then start a new one;*/int restart(){boost::lock_guard<boost::mutex> lock(_mutex);if(_thread){_thread->interrupt();_thread->join();_thread->detach();}_thread.reset(new boost::thread(_func));return 0;};/*** wait thread finished*/void join(){boost::lock_guard<boost::mutex> lock(_mutex);if(!_thread) _thread->join();};~RunOnce(){try{_thread->interrupt();//_thread->join();_thread->detach();}catch(...){}};private:boost::scoped_ptr<boost::thread> _thread; //backward thread to run functionT _func; boost::mutex _mutex;};#endif
0 0
- 使用boost::function 实现一个 单例线程
- 使用boost库实现一个单例模式
- 用c++11的bind,function,boost::condition_variable实现的一个线程安全的任务队列
- 使用boost::bind和boost::function实现工厂模式
- boost function和bind,实现Linux线程类封装
- boost function 实现
- 使用boost::function错误
- boost::function使用
- boost之function使用
- 怎么实现一个线程安全的单例模式呢?
- C++实现一个线程安全的单例工厂
- 实现一个线程安全的单例模式
- 实现一个单例模式的类,要求线程安全
- 一个类里可以注册不同类型的消息及boost:function和boost:bind的使用
- boost::function 简单实现理念
- boost::bind和boost::function使用示例
- boost::function与boost::bind 的使用
- Boost.Function的基本使用
- Windows Server 2008 R2 Oracle 11.0.2.1配置ST_Geometry -终极版
- ActiveMQ 简述
- mysql多实例安装说明
- Git8--添加远程库
- 操作系统对内存管理
- 使用boost::function 实现一个 单例线程
- 2016学习笔记(一)
- 用selenium打开谷歌浏览器提示 chromedriver.exe停止运行
- 项目中引入log4j
- App性能之优化
- 使用Studio在genymotion模拟器上运行项目时提示错误:installation failed with message INSTALL_FAILED_CPU_ABI_INCOMPATIBL
- 随机数
- 求两数的最大公因数
- 鼠标操作