ile başarısız Ben başarısız ve güvenli kapatma nedenleri hakkında bilgi sağlar, böylece, ana iş parçacığı için herhangi yakalanmamış iplik gelen istisnalar yaymak için gereken bir program var. Benim InterThreadException
işleyici çalışmak ancak bir istisna atar iplik ana iş parçacığı tarafından doğrudan kökenli yalnızca görünüyor.std :: rethrow_exception iç içe parçacığı
InterTheadException
işleyicisi geçirilen bir özel işaretçi olan bir istisna yayılır sinyallerini olan, her durumda doğru çağrılan ve ana dişli, yeni bir durum almış bildirim ancak std::rethrow_exception
için aramayı kabul ederse istisna işaretçisi üzerinde sadece başarısız olur ve görünüşte hiçbir şey yapmaz. Bu iki farklı konudan aynı istisnayı fırlatmayı test ettim ve sorun ne attığım önemli değil.
ben bir istisna işaretçileri kullanmak gerekiyordu nasıl temel bir yanlış anlama var sanıyorum, ama emin değilim.
İşte InterThreadExceptionHandler
benim uygulamasıdır.
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);
}
http://stackoverflow.com/questions/233127/how-can-i-propagate-exceptions-between-threads adresinin olası kopyası – engineerC
Sınıfınız GNU/Linux üzerinde GCC ile iyi çalışıyor (count count() işlevini değiştirdikten sonra boyut() 've standart dışı' std çağrıyı değişen :: exception' yapıcı ve değiştirme 'için' 'std :: condition_variable') ile Semaphore' yüzden bir MSVC hata gibi geliyor. Bir iş parçacığının "ana" iş parçacığı mı yoksa başka bir iş parçacığı tarafından mı ortaya çıktığı önemli değil. –
vs2013 üzerinde çalışıyor mu? –