キャッチされていないスレッドからメインスレッドに例外を伝播する必要があるプログラムがあります。これにより、失敗した理由に関する情報をユーザーに提供し、安全にシャットダウンできます。私のInterThreadException
ハンドラーは機能しているように見えますが、例外をスローするスレッドがメインスレッドによって直接生成された場合のみです。
ハンドラーはすべてのInterTheadException
場合に正しく呼び出され、例外ポインターが渡されることによって例外が伝搬されたことが通知され、メインスレッドは新しい例外を受け取ったという通知を受け取りますがstd::rethrow_exception
、例外ポインターでの呼び出しは単に失敗しますそして一見何もしません。これら 2 つの異なるスレッドからまったく同じ例外をスローすることをテストしましたが、何をスローしても問題は解決しないようです。
例外ポインターの使用方法について根本的な誤解があると思いますが、よくわかりません。
これが私の実装ですInterThreadExceptionHandler
。
class InterThreadExceptionHandler : NonCopyable
{
public:
InterThreadExceptionHandler();
~InterThreadExceptionHandler();
//
// Sends an exception into this handler, informing it that
//this exception has been caught
// and may be propagated to another thread
//
void sendException(std::exception_ptr exception);
//
// Freezes the calling thread until an exception has been sent,
//then rethrows said exception
//
void waitForException();
private:
std::vector<std::exception_ptr> sentExceptions;
std::mutex sentExceptionsMutex;
Semaphore sentExceptionsCounter;
};
InterThreadExceptionHandler::InterThreadExceptionHandler()
{
}
InterThreadExceptionHandler::~InterThreadExceptionHandler()
{
}
void InterThreadExceptionHandler::sendException(std::exception_ptr exception)
{
ScopedLock lock(this->sentExceptionsMutex);
this->sentExceptions.push_back(exception);
this->sentExceptionsCounter.give();
}
void InterThreadExceptionHandler::waitForException()
{
this->sentExceptionsCounter.take();
ScopedLock lock(this->sentExceptionsMutex);
assert( this->sentExceptions.size() > 0 );
std::exception_ptr e = this->sentExceptions[0];
this->sentExceptions.erase(this->sentExceptions.begin());
if (e == std::exception_ptr())
{
throw std::exception("Invalid exception propagated!");
}
std::rethrow_exception(e);
}