其他分享
首页 > 其他分享> > c-提升boost :: thread的指针

c-提升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.interrupt或manager.stop返回分段错误..

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 :: thread的指针不为null,那么会发生什么呢?先感谢您.

乳清,我不能做这样的事情?

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>来解决此问题.而不是线程*:

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,c
来源: https://codeday.me/bug/20191013/1907530.html