#include #include #include boost::shared_mutex mutex; void thread() { try { for (;;) { boost::system_time timeout = boost::get_system_time() + boost::posix_time::milliseconds(50); if (mutex.timed_lock(timeout)) { boost::this_thread::sleep(boost::posix_time::milliseconds(10)); mutex.unlock(); } } } catch (boost::lock_error& le) { std::cerr << "lock_error exception\n"; } } int main() { const int nrThreads = 20; boost::thread* threads[nrThreads]; for (int i = 0; i < nrThreads; ++i) threads[i] = new boost::thread(&thread); for (int i = 0; i < nrThreads; ++i) { threads[i]->join(); delete threads[i]; } return 0; }