1 // Test whether no race conditions are reported on std::thread. Note: since
2 // the implementation of std::thread uses the shared pointer implementation,
3 // that implementation has to be annotated in order to avoid false positives.
4 // See also http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug.html for more
7 #include "../../drd/drd.h"
8 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(addr) \
9 ANNOTATE_HAPPENS_BEFORE(addr)
10 #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(addr) \
11 ANNOTATE_HAPPENS_AFTER(addr)
19 int main(int argc
, char** argv
)
21 std::thread
t1( []() { sleep(1); i
= 1; } );
22 std::thread
t2( []() { i
= 2; } );
25 std::cerr
<< "Done.\n";
29 #if defined(__GNUC__) && __GNUC__ -0 < 6
31 // From libstdc++-v3/src/c++11/thread.cc
34 extern "C" void* _v_execute_native_thread_routine(void* __p
)
36 std::thread::_Impl_base
* __t
= static_cast<std::thread::_Impl_base
*>(__p
);
37 std::thread::__shared_base_type __local
;
38 __local
.swap(__t
->_M_this_ptr
);
42 } __catch(const __cxxabiv1::__forced_unwind
&) {
43 __throw_exception_again
;
51 #include <system_error>
55 void thread::_M_start_thread(__shared_base_type __b
)
57 if (!__gthread_active_p())
59 throw system_error(make_error_code(errc::operation_not_permitted
),
60 "Enable multithreading to use std::thread");
62 __throw_system_error(int(errc::operation_not_permitted
));
65 __b
->_M_this_ptr
= __b
;
66 int __e
= __gthread_create(&_M_id
._M_thread
, _v_execute_native_thread_routine
,
69 __b
->_M_this_ptr
.reset();
70 __throw_system_error(__e
);