#include #include #include #include #include #include #include static const std::size_t threadCount = 10; static void threadMain( boost::barrier& barrier, boost::mutex& mutex, std::size_t& sharedCounter, bool& errorFound) { for (std::size_t i = 0; i < 20; ++i) { { boost::mutex::scoped_lock g(mutex); ++sharedCounter; } if (barrier.wait()) { if (sharedCounter != (i + 1) * threadCount) { errorFound = true; } } barrier.wait(); } } extern "C" { int function() { int result = 0; try { boost::thread_group threads; boost::barrier barrier(threadCount); boost::mutex mutex; std::size_t sharedCounter = 0; bool errorFound = false; for (std::size_t i = 1; i < threadCount; ++i) { threads.create_thread( boost::bind( threadMain, boost::ref(barrier), boost::ref(mutex), boost::ref(sharedCounter), boost::ref(errorFound))); } threadMain(barrier, mutex, sharedCounter, errorFound); threads.join_all(); if (errorFound) { result = -1; } } catch (std::exception& e) { std::cerr << "Error: " << boost::current_exception_diagnostic_information() << std::endl; result = -1; } return result; } }