2

簡略化した次のコードがあります。

スレッド2:

  boost::unique_future<void> future;
  TaskPtr task(new task::ReloadConfig(future));
  Listener::PushTask(task);
  future.wait();

  try
  {
    future.get();
  }
  catch (const cfg::ConfigError& e)
  {
    return cmd::Result::Okay;
  }

スレッド2:

  try
  {
    cfg::UpdateShared(std::shared_ptr<cfg::Config>(new cfg::Config(configFile)));
  }
  catch (...) // should be cfg::ConfigError
  {
    promise.set_exception(boost::current_exception());
    return;
  }

  promise.set_value();

Cfg :: ConfigError例外またはその派生例外の1つがスレッド2からスレッド1に伝播される代わりに、次のようになります。

'boost :: exception_detail :: clone_impl'
what():std::exceptionのインスタンスをスローした後に呼び出された終了

この他の人も同様の問題を抱えていたようで、答えはありません:

https://stackoverflow.com/questions/10857834/how-to-use-boostfuture-get-to-get-user-defined-exception

また、boost :: enable_current_exceptionを使用しようとすると、次のエラーが発生します。

/usr/local/include/boost/exception/exception.hpp:419:20:エラー:「std :: runtime_error :: runtime_error()」の呼び出しに一致する関数がありません

ブール値を返すだけで、例外なくコードを正常に動作させることができますが、これは妥協点です。

4

1 に答える 1

0

私はあなたのスレッド2でそのようなことをしようとします:

catch (const cfg::ConfigError& e)
{
    promise.set_exception(boost::make_exception_ptr(e));
}

それ以外の場合は、次current_exception()のようなものをいじる必要があります。

throw boost::enable_current_exception(cfg::ConfigError("meh")); 
于 2012-11-05T06:15:02.743 に答える