Add DRD suppression patterns for races triggered by std::ostream
[valgrind.git] / drd / tests / std_thread2.cpp
blob5a4aff929476659c4a47c57af60555700befd00b
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
5 // information.
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)
13 #include <iostream>
14 #include <thread>
15 #include <unistd.h>
17 static int i;
19 int main(int argc, char** argv)
21 std::thread t1( []() { sleep(1); i = 1; } );
22 std::thread t2( []() { i = 2; } );
23 t2.join();
24 t1.join();
25 std::cerr << "Done.\n";
26 return 0;
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);
40 __try {
41 __t->_M_run();
42 } __catch(const __cxxabiv1::__forced_unwind&) {
43 __throw_exception_again;
44 } __catch(...) {
45 std::terminate();
48 return 0;
51 #include <system_error>
53 namespace std
55 void thread::_M_start_thread(__shared_base_type __b)
57 if (!__gthread_active_p())
58 #if __EXCEPTIONS
59 throw system_error(make_error_code(errc::operation_not_permitted),
60 "Enable multithreading to use std::thread");
61 #else
62 __throw_system_error(int(errc::operation_not_permitted));
63 #endif
65 __b->_M_this_ptr = __b;
66 int __e = __gthread_create(&_M_id._M_thread, _v_execute_native_thread_routine,
67 __b.get());
68 if (__e) {
69 __b->_M_this_ptr.reset();
70 __throw_system_error(__e);
74 #endif