boost :: thread的指针 [英] Pointer to boost::thread

查看:225
本文介绍了boost :: thread的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于线程管理的问题。



我的问题是,我想创建一个类ThreadManager必须管理所有创建的线程,当然也销毁此线程。

  class DerivedInterface 
{
public:
DerivedInterface():id (测试){};
virtual〜DerivedInterface(){};
virtual void run()= 0;
virtual std :: string getId()= 0;
const std :: string id;
};

类对象:public DerivedInterface
{
public:
Object():id(VirtualDae){};
〜Object(){}

void run()
{

std :: cout< ;< std :: endl;

bool flag = true;

while(flag){
//分配x资源

try {
//对资源做一些处理
boost :: this_thread :: sleep(boost :: posix_time :: milliseconds(100));
//清理资源
}
catch(boost :: thread_interrupted const&)
{
//清理资源
std :: cout< ; 工作线程中断< 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);

}

}
/ *
*此方法将线程的指针插入
*映射
* /

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));
}

/ *
*此方法返回线程的指针
*插入到映射
* /

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;
}
}


/ *
*此方法停止线程将
*转换为位置pos作为映射中的参数
* /
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<< 工作者线程完成< 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< 工作者线程完成< std :: endl;
}
else
{
//未完成;
std :: cout<< 工作者线程未完成< std :: endl;
}

}

函数t1.interrupt .stop return segmantation fault。

 程序终止于信号11,分段故障。在boost :: thread :: thread :: get_thread_info()const()从libboost_thread.so.1.51.0 
(gdb)
#0 0x00007f3e1d095993其中
#0 0x00007f3e1d095993在boost :: thread :: get_thread_info()const()from libboost_thread.so.1.51.0
#1 0x00007f3e1d0965c6在boost :: thread :: interrupt()()从libboost_thread.so.1.51.0
#2 0x00000000004088a9在main ()

boost :: thread的指针不为null,先谢谢。



乳清我不能这样做?

  boost :: thread * t1 = new boost :: thread(& DerivedInterface :: run,& t); 


解决方案

 (DerivedInterface& t)
{
boost :: thread t1(& DerivedInterface :: run,& t);
insert(& t1);
}

在上述 t1 对象在堆栈上创建,然后指向 t1 的指针被插入到映射中,然后 t1



你可以通过传递 shared_ptr< thread> 而不是线程*

 :: 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 为你做上面的。


I have a problem about the managed of thread.

My problem is that I want to create a class ThreadManager that have to manage all thread created and of course also destroy this thread.

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;
}

     }  

the function t1.interrupt or manager.stop return segmantation fault ..

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 ()

Pointer of boost::thread is not null, so what happen? Thank you in advance.

Whey I can't do something like this ?

boost::thread *t1 = new boost::thread(&DerivedInterface::run, &t);

解决方案

void createThread(DerivedInterface& t)
{
    boost::thread t1(&DerivedInterface::run, &t); 
    insert(&t1);
}

In the above t1 object gets created on the stack, then a pointer to t1 gets inserted into the map and then t1 goes out of scope and gets destroyed, so that all existing pointers to it become invalid.

You can fix it by passing around shared_ptr<thread> instead of 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);
}

Or, by using r-value references in C++11:

void insert(boost::thread&& t1);

void createThread(DerivedInterface& t)
{
    boost::thread t1(&DerivedInterface::run, &t); 
    insert(std::move(t1));
}

Alternatively, use boost::thread_group that does the above for you.

这篇关于boost :: thread的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆