[20160216] 自带中断点的线程模型
来源:互联网 发布:淘宝退运费怎么退 编辑:程序博客网 时间:2024/05/10 11:23
由于std::thread无法在运行时被其他线程强制结束, 鉴于业务需要,不得不拐弯抹角换一种新方式来实现。于是今天写了点关于这个的代码
(然而仍然存在一些问题)
class procx /** HC TECH : MutableThread Version 0.1 ( build 1 , 20160216 )*/{public: procx(auto a) { func=a; cancel=false; done=false; status_code=0; s=nullptr; } ~procx() { delete s; } template <typename ... ARG> void start(ARG ... arg) { s=new std::thread(func,arg...); } void join() { if(s!=nullptr&&s->joinable()) { s->join(); } } void detach() { if(s!=nullptr&&s->joinable()) { s->detach(); } } bool isdone() { return done; } bool iscanceled() { return cancel; } int getcancelcode() { return status_code; } /// return: true : Thread is canceled. false: Thread is over. else will be blocked. bool trycancel() { cancel=true; join(); return done; }private: void* func; std::thread* s; bool cancel; bool done; int status_code;};
procx类就是一个线程管理的类。
如果一个线程函数希望能够被cancel,那么这个方法在编写时需要使用如下的宏定义
#define DECLX bool* doneflag,bool* cancelflag,int* _hc_status_code#define SETUNDONE *doneflag=false;#define SETDONE *doneflag=true;#define CANCELPOINT(x) if(*cancelflag){*_hc_status_code=x;return;}
一个比较正规的写法如下
void proc_a(DECLX,int* ret){ SETUNDONE CANCELPOINT(1); SETDONE}
如果整个代码能够工作,那么使用方法应该是这样的
int main(){ int ret=0; procx s(proc_a,&ret); /// Start Job "s" s.start(); /// Try to cancel Job "s" s.trycancel();}
然而目前还是无法通过编译,看来通过C++模板方式支持函数参数转发,变长参数这些知识我还是掌握的不太好。继续努力!
1 0
- [20160216] 自带中断点的线程模型
- java自带的线程池ThreadPoolExecutor
- Java底层自带的线程池
- java自带的线程池ThreadPoolExecutor
- Java自带的线程池ThreadPoolExecutor
- JDK自带的线程池
- java自带线程池的使用
- JVM 自带线程
- OSG中显示模型自带的动画
- Xcode中断点的威力
- Xcode中断点的威力
- Xcode中断点的威力
- Xcode中断点的威力
- Xcode中断点的威力
- Xcode中断点的威力
- Xcode中断点的威力
- Xcode中断点的威力
- gdb中断点的使用
- 信息提醒之对话框(AlertDialog + ProgressDialog)-更新中
- HDOJ 1005 Number Sequence
- unity shader简介与入门
- 51nod 1405 树的距离之和
- nefuoj-549:气球
- [20160216] 自带中断点的线程模型
- 太阳神电商业务辅助工具1.5
- Java学习之静态修饰符:static
- 启动其他Activity并返回结果 非常简单 只是有一些细节
- UVA 839
- parse()与stringify()
- 从头认识Spring-2.6 在注解中使用表达式@Value(2)-使用表达式注入其他对象的值
- https详解
- web过滤器