112751Sqtt2@cornell.edu/* 212751Sqtt2@cornell.edu * Copyright (c) 2018, Cornell University 312751Sqtt2@cornell.edu * All rights reserved. 412751Sqtt2@cornell.edu * 512751Sqtt2@cornell.edu * Redistribution and use in source and binary forms, with or 612751Sqtt2@cornell.edu * without modification, are permitted provided that the following 712751Sqtt2@cornell.edu * conditions are met: 812751Sqtt2@cornell.edu * 912751Sqtt2@cornell.edu * Redistributions of source code must retain the above copyright 1012751Sqtt2@cornell.edu * notice, this list of conditions and the following disclaimer. 1112751Sqtt2@cornell.edu * 1212751Sqtt2@cornell.edu * Redistributions in binary form must reproduce the above 1312751Sqtt2@cornell.edu * copyright notice, this list of conditions and the following 1412751Sqtt2@cornell.edu * disclaimer in the documentation and/or other materials provided 1512751Sqtt2@cornell.edu * with the distribution. 1612751Sqtt2@cornell.edu * 1712751Sqtt2@cornell.edu * Neither the name of Cornell University nor the names of its 1812751Sqtt2@cornell.edu * contributors may be used to endorse or promote products derived 1912751Sqtt2@cornell.edu * from this software without specific prior written permission. 2012751Sqtt2@cornell.edu * 2112751Sqtt2@cornell.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 2212751Sqtt2@cornell.edu * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 2312751Sqtt2@cornell.edu * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2412751Sqtt2@cornell.edu * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2512751Sqtt2@cornell.edu * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 2612751Sqtt2@cornell.edu * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2712751Sqtt2@cornell.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2812751Sqtt2@cornell.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2912751Sqtt2@cornell.edu * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 3012751Sqtt2@cornell.edu * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112751Sqtt2@cornell.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 3212751Sqtt2@cornell.edu * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3312751Sqtt2@cornell.edu * POSSIBILITY OF SUCH DAMAGE. 3412751Sqtt2@cornell.edu * 3512751Sqtt2@cornell.edu * Authors: Tuan Ta, Moyang Wang 3612751Sqtt2@cornell.edu */ 3712751Sqtt2@cornell.edu 3812751Sqtt2@cornell.edu#include <cstdlib> 3912751Sqtt2@cornell.edu#include <iostream> 4012751Sqtt2@cornell.edu#include <mutex> 4112751Sqtt2@cornell.edu#include <thread> 4212751Sqtt2@cornell.edu#include <vector> 4312751Sqtt2@cornell.edu 4412751Sqtt2@cornell.edu//------------------------------------------------------------------------ 4512751Sqtt2@cornell.edu// Create n threads, run them in parallel and wait for them in the master 4612751Sqtt2@cornell.edu// thread. 4712751Sqtt2@cornell.edu// Each child thread increments a shared variable m times 4812751Sqtt2@cornell.edu//------------------------------------------------------------------------ 4912751Sqtt2@cornell.edu 5012751Sqtt2@cornell.edu#define MAX_N_WORKER_THREADS 10 5112751Sqtt2@cornell.edu 5212751Sqtt2@cornell.eduint main( int argc, const char* argv[] ) 5312751Sqtt2@cornell.edu{ 5412751Sqtt2@cornell.edu int n_worker_threads = 0; 5512751Sqtt2@cornell.edu 5612751Sqtt2@cornell.edu // allocate all threads 5712751Sqtt2@cornell.edu std::vector< std::thread > threads; 5812751Sqtt2@cornell.edu 5912751Sqtt2@cornell.edu // mutex to protect the shared variable 6012751Sqtt2@cornell.edu std::mutex my_mutex; 6112751Sqtt2@cornell.edu 6212751Sqtt2@cornell.edu // variable shared among all threads 6312751Sqtt2@cornell.edu int shared_var = 0; 6412751Sqtt2@cornell.edu 6512751Sqtt2@cornell.edu // number of steps each thread increments the shared_var 6612751Sqtt2@cornell.edu int nsteps = 1000; 6712751Sqtt2@cornell.edu 6812751Sqtt2@cornell.edu for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { 6912751Sqtt2@cornell.edu try { 7012751Sqtt2@cornell.edu threads.push_back( std::thread( [&] { 7112751Sqtt2@cornell.edu std::lock_guard<std::mutex> guard(my_mutex); 7212751Sqtt2@cornell.edu for ( int i = 0; i < nsteps; ++i ) 7312751Sqtt2@cornell.edu shared_var++; 7412751Sqtt2@cornell.edu } ) ); 7512751Sqtt2@cornell.edu } catch ( const std::system_error& err ) { 7612751Sqtt2@cornell.edu break; 7712751Sqtt2@cornell.edu } 7812751Sqtt2@cornell.edu n_worker_threads++; 7912751Sqtt2@cornell.edu } 8012751Sqtt2@cornell.edu 8112751Sqtt2@cornell.edu // sync up all threads 8212751Sqtt2@cornell.edu for (int i = 0; i < n_worker_threads; ++i) { 8312751Sqtt2@cornell.edu threads[i].join(); 8412751Sqtt2@cornell.edu } 8512751Sqtt2@cornell.edu 8612751Sqtt2@cornell.edu // verify 8712751Sqtt2@cornell.edu if ( shared_var != n_worker_threads * nsteps || n_worker_threads < 1) { 8812751Sqtt2@cornell.edu return EXIT_FAILURE; 8912751Sqtt2@cornell.edu } 9012751Sqtt2@cornell.edu 9112751Sqtt2@cornell.edu return EXIT_SUCCESS; 9212751Sqtt2@cornell.edu} 93