私のコードではboost::threads
を使用し、fnThread()
というメンバ関数を介してスレッドを実行するクラスを持っています。このfnThread()では、shared_from_this()を作成し、リスニングパーティーにシグナルで渡したいと思います。しかし、行boost::shared_ptr<foo> p = shared_from_this()
は次のように例外をスローします。他のスレッド(メンバースレッド関数)のshared_from_this()
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> > at memory location 0x04c2f720.
違反コマンドはshared_from_this()です。別のスレッドからshared_from_this()
を作成するのは違法ですか?何か間違っていますか?どんな助けもありがとうございます。ありがとう!
PS:自分の構造に影響を与えないので、シグナルの引数をプレーンポインタに変更する予定です。しかし、私はshared_ptr <を好んでおり、その決定に関するコメントを聞きたいと思っています。この特定の状況に対してはおそらくそれは悪い選択です。何を指示してるんですか ?
EDIT:あなたは
class foo : public boost::enable_shared_from_this<foo>
{
public:
int start()
{
foo_thread.reset(new boost::thread(boost::bind(&foo::fn_foo_thread, this)));
return 0;
}
~foo()
{
if (foo_thread->joinable())
foo_thread->join();
}
private:
boost::scoped_ptr<boost::thread> foo_thread;
void fn_foo_thread()
{
boost::shared_ptr<foo> p = shared_from_this();
std::cout << "foo thread terminated. \n" << std::endl;
}
};
「あなたはshared_from_this()を作成しますか?」とはどういう意味ですか? –
私は 'boost :: shared_ptr 'を' this 'にすることを意味します –
fgungor
' foo'をインスタンス化し、 'start'を呼び出すコードを示してください。間違いはおそらくそこにあります。 – Macke