简体   繁体   English

对创建的shared_ptr的boost :: make_shared引用

[英]boost::make_shared reference to created shared_ptr

I want to make another thread in my application, so I'm doing it that way: 我想在我的应用程序中创建另一个线程,所以我这样做:

typedef boost::shared_ptr<boost::thread> wild_thread;

void thread_routine(wild_thread t) {
    // do stuff
}
int main() {
    {
        wild_thread t;
        t.reset(new boost::thread(boost::bind(&thread_routine, t)));
    }
    // do other stuff
}

But this is ugly, I need to name this temporary shared_ptr. 但这很丑陋,我需要将此临时共享名命名为。

So, the question is, can I do this with boost::make_shared anyhow? 所以,问题是,我是否可以通过boost::make_shared做到这一点? Can I somehow ask it to bind newly created shared_ptr into my thread_routine? 我可以以某种方式要求它将新创建的shared_ptr绑定到我的thread_routine中吗? Or maybe there is a better way? 还是有更好的方法?

You can't pass the thread pointer t into your thread_routine with boost::bind because t isn't initialized until after the thread has been created, which is after the boost::bind call has returned. 您不能使用boost::bind将线程指针t传递到thread_routine ,因为直到创建线程之后(即boost::bind调用返回之后), t才被初始化。

You should try to avoid needing a pointer to the thread object from within the thread itself. 您应该尝试避免需要从线程本身内部指向线程对象的指针。 Take a look at the functions in the boost::this_thread namespace instead. 请看一下boost::this_thread命名空间中的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM