Answer the question
In order to leave comments, you need to log in
Call current_exception in parallel on one exception object
I would like to turn to the community to clarify the situation that is not entirely clear to me.
We are talking about exception_ptr, rethrow_exception and current_exception in boost. The essence of the problem is the following: should it be thread-safe to execute current_exception (in multiple threads) after rethrow_exception for a shared exception object.
#include <iostream>
#include <exception>
#include <stdexcept>
#include <vector>
//#define USE_CPP11
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <thread>
#include <future>
#include <memory>
#else
#include <boost>
#include <boost>
using namespace boost;
#endif
using namespace std;
void executer(mutex& mtx, exception_ptr ex)
{
for(int i = 0; i < 9999999; i++)
{
try
{
lock_guard<mutex> lck(mtx);
rethrow_exception(ex);
}
catch(...)
{
current_exception(); // <- run concurrently
}
}
}
void test()
{
exception_ptr ex;
try
{
throw runtime_error("O_o");
}
catch(...)
{
// use current_exception() it's compulsory condition
// because only in this case will be used unsafe detail::refcount_ptr
// (in current_exception_std_exception_wrapper inherited from exception)
ex = current_exception();
}
mutex mtx;
vector<shared_ptr> > group;
for(int i = 0; i < 10; i++)
group.push_back(shared_ptr<thread>(new thread(bind(executer, ref(mtx), ex))));
vector<shared_ptr> >::iterator it = group.begin();
for(; it != group.end(); it++)
(*it)->join();
}
void version()
{
#ifdef __GXX_EXPERIMENTAL_CXX0X__
cout << "use c++11 implementation\n";
#else
cout << "use boost implementation\n";
#endif
}
int main()
{
version();
cout << "Start\n";
test();
cout << "Exit\n";
}
</shared_ptr></thread></shared_ptr></mutex></boost></boost></memory></future></thread></vector></stdexcept></exception></iostream>
this example crashes if boost is used and doesn't crash if std implementation is used (-std=c++11).
The standard says the following about this:
18.8.5 Exception propagation ... For purposes of determining the presence of a data race, operations oneexception_ptr objects shall access and modify only the exception_ptrobjects themselves and not the exceptions they refer to. Use of rethrow_exceptionon exception_ptr objects that refer to the same exception object shallnot introduce a data race. [Note: if rethrow_exception rethrows the same exception object (ratherthan a copy), concurrent access to that rethrown exception object may introduce a data race. Changesin the number ofexception_ptrobjects that refer to a particular exception do not introduce a datarace.—end note]
What is described in the note is just my situation, but I don’t understand, should it be like this or shouldn’t it be?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question