Pointer to boost::thread
Pointer to boost::thread
我对线程的管理有问题。
我的问题是我想创建一个类 ThreadManager,它必须管理创建的所有线程,当然还要破坏这个线程。
class DerivedInterface
{
public:
DerivedInterface():id("Test"){};
virtual ~DerivedInterface(){};
virtual void run() = 0;
virtual std::string getId() = 0;
const std::string id ;
};
class Object : public DerivedInterface
{
public:
Object():id("VirtualDae"){};
~Object(){}
void run()
{
std::cout<<"i'M IN RUN"<<std::endl;
bool flag = true;
while(flag){
//allocate x resources
try{
//do some process on resources
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
//clean resources
}
catch(boost::thread_interrupted const& )
{
//clean resources
std::cout << "Worker thread interrupted" << std::endl;
flag = false;
}
catch(std::exception x){
std::cout<<"exx"<<x.what()<<std::endl;
}
}
}
std::string getId(){
return id;
}
const std::string id ;
};
class ThreadManager
{
public:
void createThread(DerivedInterface& t)
{
boost::thread t1(&DerivedInterface::run, &t);
insert(&t1);
}
}
/*
* This method insert the pointer of the thread in a
* map
*/
void insert(boost::thread* t1)
{
boost::mutex::scoped_lock lock(m_mutex);
int size = threadsMap.size()+1;
std::cout<<"Size :"<<size<<std::endl;
threadsMap.insert(std::make_pair(size, t1));
}
/*
* This method return the pointer of the thread
* inserted in a map
*/
boost::thread* get(int key){
boost::mutex::scoped_lock lock(m_mutex);
if(threadsMap.find(key)!=threadsMap.end()){
std::cout<<"non null get"<<std::endl;
return threadsMap[key];
}else{
std::cout<<" null get"<<std::endl;
return NULL;
}
}
/*
* This method stop the thread corrisponding
* to the position pos as parameter in the map
*/
void stop(int pos){
std::cout<<"Stop"<<std::endl;
boost::thread* thread = get(pos);
std::cout<<"thread null"<<std::endl;
if(thread != NULL)
{
std::cout<<"thread not null"<<std::endl;
thread->interrupt();
std::cout << "Worker thread finished" << std::endl;
}
}
private:
boost::shared_ptr<boost::thread> _mThread;
typedef std::map<int, boost::thread*> ThreadMapT;
ThreadMapT threadsMap;
std::map<int,boost::thread*>::iterator it;
boost::mutex m_mutex;
boost::thread_group g;
};
int main(){
ThreadManager manager;
Object v;
//
manager.createThread(v);
std::cout<<"Interrupt"<<std::endl;
boost::thread *t1= manager.get(1);
t1->interrupt();
//
boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(10000);
if (manager.get(1)->timed_join(timeout))
{
//finished
std::cout << "Worker thread finished" << std::endl;
}
else
{
//Not finished;
std::cout << "Worker thread not finished" << std::endl;
}
}
函数 t1.中断或管理器.停止返回隔离错误 ..
Program terminated with signal 11, Segmentation fault.
#0 0x00007f3e1d095993 in boost::thread::get_thread_info() const () from libboost_thread.so.1.51.0
(gdb) where
#0 0x00007f3e1d095993 in boost::thread::get_thread_info() const () from libboost_thread.so.1.51.0
#1 0x00007f3e1d0965c6 in boost::thread::interrupt() () from libboost_thread.so.1.51.0
#2 0x00000000004088a9 in main ()
boost::线程的指针不为空,那么会发生什么?提前谢谢你。
乳清我不能做这样的事情吗?
boost::thread *t1 = new boost::thread(&DerivedInterface::run, &t);
void createThread(DerivedInterface& t)
{
boost::thread t1(&DerivedInterface::run, &t);
insert(&t1);
}
在上面的t1
对象是在堆栈上创建的,然后指向t1
的指针入到映射中,然后t1
超出范围并被销毁,因此所有指向它的现有指针都变得无效。
您可以通过传递shared_ptr<thread>
而不是thread*
来修复它:
void insert(boost::shared_ptr<boost::thread>);
void createThread(DerivedInterface& t)
{
boost::shared_ptr<boost::thread> t1(new boost::thread(&DerivedInterface::run, &t));
insert(t1);
}
或者,通过在 C++11 中使用 r 值引用:
void insert(boost::thread&& t1);
void createThread(DerivedInterface& t)
{
boost::thread t1(&DerivedInterface::run, &t);
insert(std::move(t1));
}
或者,使用为您执行上述操作的boost::thread_group
。
相关文章:
- 无法将参数传递给 boost::thread 构造函数
- 如何使用boost.thread运行多个接收器,每个线程应该分配给每个接收器?
- 使用 boost::thread 运行函子,但析构函数被错误地调用
- 最佳实践:我应该使用 std::thread、boost 还是本机调用
- boost::asio io_service thread pool with io_service::work
- 在thread.hpp上boost asio udp 螺纹= get_id()错误
- thread_specific_pointer可以在哪些平台上工作而无需 boost::thread?
- 将 boost::thread 与 C++11 std::mutex 混合使用是否安全?
- 同时使用 boost.python 和 boost.thread 时出现编译错误
- Boost.thread代码在Ubuntu和Windows中表现出不同的行为
- Pointer to boost::thread
- Linux上多核计算的低性能(openMP、boost::thread等)
- boost - thread.join() halts the ui
- 将基类的引用传递给 boost::thread 并在派生类中调用虚函数是否有效
- Difference between boost::thread and std::thread
- std::thread是boost::thread的替代品吗?
- 将 boost/thread headers 与 clang++ (Windows) 一起使用时出现问题
- 在linux下使用boost::thread创建一个boost::asio工作线程
- "Thread" Boost 无法在 OSX 上构建?
- C++ Boost::Thread & Boost::ASIO Memory leak