// (C) Copyright 2013 Ruslan Baratov // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // See www.boost.org/libs/thread for documentation. #include #include #include boost::mutex m; // protection for 'x' and 'std::cout' int x; void job() { for (int i = 0; i < 10; ++i) { boost::with_lock_guard( m, []() { ++x; std::cout << "x = " << x << std::endl; } ); boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } } int main() { boost::scoped_thread<> thread_1(job); boost::scoped_thread<> thread_2(job); boost::scoped_thread<> thread_3(job); }