lundi 2 mars 2015

Thrown object cannot be caught in a multi-threaded solution

I have a RAII class that solves a problem in an inner thread:



#include <iostream>
#include <thread>
using namespace std;

struct solution_using_thread {
solution_using_thread()
: alive_(true), thread_() {
thread_ = thread([this]() {
while(alive_);
});
}
~solution_using_thread() {
alive_ = false;
thread_.join();
}
private:
bool alive_;
thread thread_;
};

int main() {
cout << 0 << endl;
try {
solution_using_thread solution;
throw 1;
} catch (int i ) {
cout << i << endl;
}
cout << 2 << endl;
}


After creating an instance of it, the main happens to throw an exception. The problem is in some of the executions, the output is:



0


when it always should have been:



0
1
2


In coliru, it is always only 0.


What am I missing here?


PS: Sorry, for the ambiguous title. Please do not hesitate to suggest a better one for this question.


Aucun commentaire:

Enregistrer un commentaire