// (C) Copyright 2013 Ruslan Baratov// Copyright (C) 2014 Vicente Botet//// 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.#define BOOST_THREAD_VERSION 4#include <iostream> // std::cout#include <boost/thread/scoped_thread.hpp>#include <boost/thread/with_lock_guard.hpp>boost::mutex m; // protection for 'x' and 'std::cout'int x;#if defined(BOOST_NO_CXX11_LAMBDAS) || (defined BOOST_MSVC && _MSC_VER < 1700)void print_x() {++x;std::cout << "x = " << x << std::endl;}void job() {for (int i = 0; i < 10; ++i) {boost::with_lock_guard(m, print_x);boost::this_thread::sleep_for(boost::chrono::milliseconds(100));}}#elsevoid 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));}}#endifint main() {#if defined(BOOST_NO_CXX11_LAMBDAS) || (defined BOOST_MSVC && _MSC_VER < 1700)std::cout << "(no lambdas)" << std::endl;#endifboost::scoped_thread<> thread_1((boost::thread(job)));boost::scoped_thread<> thread_2((boost::thread(job)));boost::scoped_thread<> thread_3((boost::thread(job)));return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。