• 全部
  • 问答

阿修罗的线程类(以boost::thread为基础,增加了终止线程的功能)

anrxhzh 2002-07-01 04:50:15
//阿修罗的线程类(以boost::thread为基础,增加了终止线程的功能)
//欢迎指正

//module.h
#ifndef ASHURA_MODULE_HEADER
#define ASHURA_MODULE_HEADER

#include <boost/utility.hpp>

namespace ashura {

class controlled_module_implement;
class controlled_module : boost::noncopyable {
public:
virtual void run();
bool exit(unsigned long sec=0);
protected:
controlled_module();
virtual ~controlled_module();
private:
virtual bool initialize()=0;
virtual void release()=0;
virtual bool work()=0;
private:
controlled_module_implement* impl_;
};

}

#endif

//module.cpp
#pragma warning (disable:4786)
#include "module.h"
#include "janitor.h"
#include <boost/thread/xtime.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>

namespace ashura {

class controlled_module_implement : boost::noncopyable {
public:
controlled_module_implement() :active_(false),command_exit_(false){}
boost::condition module_is_exit;
boost::mutex monitor;
void active(bool ac)
{boost::mutex::scoped_lock lk(monitor); if(!(active_=ac))module_is_exit.notify_all();else command_exit_=false;}
bool command_exit(){boost::mutex::scoped_lock lk(monitor); return command_exit_;}
bool active_,command_exit_;

};

controlled_module::controlled_module()
:impl_(new controlled_module_implement)
{
}

controlled_module::~controlled_module()
{
delete impl_;
}

void controlled_module::run()
{
ObjJanitor janitor(*impl_,&controlled_module_implement::active,false);
impl_->active(true);
{
ObjJanitor janitor(*this,&controlled_module::release);
if(this->initialize())
{
while(!impl_->command_exit() && this->work());
}
}
}

bool controlled_module::exit(unsigned long sec)
{
boost::mutex::scoped_lock lk(impl_->monitor);
impl_->command_exit_ = true;
while(impl_->active_)
{
if(sec)
{
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += sec;
if(!impl_->module_is_exit.timed_wait(lk,xt))
return false;
}
else
impl_->module_is_exit.wait(lk);
}
return true;
}

}

//main.cpp
#include "module.h"
#include <boost/thread/thread.hpp>
#include <iostream>
#include <boost/thread/xtime.hpp>
#include <windows.h>

struct test : public ashura::controlled_module {
test():i(0){};
bool initialize(){std::cout<<"initialize\n";return true;}
bool work(){std::cout<<i<<std::endl;Sleep(1000);return i++<6;}
void release(){std::cout<<"release\n";};
private:
int i;
};

static test module;

void func()
{
module.run();
}

int main()
{
for(int i=0;i<3;i++)
{
boost::thread t(&func);
Sleep(10);
std::cout << "exit module " << module.exit() << std::endl;
t.join();
std::cout << std::endl;
}
return 0;
}

//测试结果
initialize
0
release
exit module 1

initialize
1
release
exit module 1

initialize
2
release
exit module 1
...全文
78 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
denghby 2002-07-14
hehe ~~~~~~~~~~~

up
回复
nikitar 2002-07-14
d
回复
wkoji 2002-07-14
UP
回复
anrxhzh 2002-07-12
哪位好心人顶一下,让俺把这个帖子结了。多谢了。
回复
anrxhzh 2002-07-01
增加了运行模块的单实例限制。

class controlled_module_implement : boost::noncopyable {
public:
boost::mutex monitor,instance;
...
};

void controlled_module::run()
{
boost::mutex::scoped_lock lk(impl_->instance);
...
}
回复
相关推荐
发帖
C语言
创建于2007-09-28

6.3w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2002-07-01 04:50
社区公告
暂无公告