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: Moyang Wang 3612751Sqtt2@cornell.edu */ 3712751Sqtt2@cornell.edu 3812751Sqtt2@cornell.edu#include <cstdlib> 3912751Sqtt2@cornell.edu#include <iostream> 4012751Sqtt2@cornell.edu#include <string> 4112751Sqtt2@cornell.edu#include <thread> 4212751Sqtt2@cornell.edu#include <vector> 4312751Sqtt2@cornell.edu 4412751Sqtt2@cornell.edu//------------------------------------------------------------------------ 4512751Sqtt2@cornell.edu// Test std::thread 4612751Sqtt2@cornell.edu//------------------------------------------------------------------------ 4712751Sqtt2@cornell.edu// Create n threads, run them in parallel and wait for them in the master 4812751Sqtt2@cornell.edu// thread. 4912751Sqtt2@cornell.edu// Each child thread writes its thread id to an output array 5012751Sqtt2@cornell.edu 5112751Sqtt2@cornell.edu#define MAX_N_WORKER_THREADS 10 5212751Sqtt2@cornell.edu 5312751Sqtt2@cornell.eduint main( int argc, char* argv[] ) 5412751Sqtt2@cornell.edu{ 5512751Sqtt2@cornell.edu int n_worker_threads = 0; 5612751Sqtt2@cornell.edu 5712751Sqtt2@cornell.edu std::vector< std::thread > threads; 5812751Sqtt2@cornell.edu std::vector<int> outputs( MAX_N_WORKER_THREADS, 0 ); 5912751Sqtt2@cornell.edu 6012751Sqtt2@cornell.edu for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) { 6112751Sqtt2@cornell.edu try { 6212751Sqtt2@cornell.edu threads.push_back( std::thread( [&] (size_t thread_id ) { 6312751Sqtt2@cornell.edu std::cout << "Hello from thread " << thread_id 6412751Sqtt2@cornell.edu << std::endl; 6512751Sqtt2@cornell.edu outputs[thread_id] = thread_id; 6612751Sqtt2@cornell.edu }, tid ) ); 6712751Sqtt2@cornell.edu } catch ( const std::system_error& err ) { 6812751Sqtt2@cornell.edu break; 6912751Sqtt2@cornell.edu } 7012751Sqtt2@cornell.edu n_worker_threads++; 7112751Sqtt2@cornell.edu } 7212751Sqtt2@cornell.edu 7312751Sqtt2@cornell.edu std::cout << "Hello from master thread" << std::endl; 7412751Sqtt2@cornell.edu 7512751Sqtt2@cornell.edu // sync up all threads 7612751Sqtt2@cornell.edu for (int i = 0; i < n_worker_threads; ++i) { 7712751Sqtt2@cornell.edu threads[i].join(); 7812751Sqtt2@cornell.edu } 7912751Sqtt2@cornell.edu 8012751Sqtt2@cornell.edu if (n_worker_threads < 1) { 8112751Sqtt2@cornell.edu return EXIT_FAILURE; 8212751Sqtt2@cornell.edu } 8312751Sqtt2@cornell.edu 8412751Sqtt2@cornell.edu for ( int i = 0; i < n_worker_threads; ++i ) { 8512751Sqtt2@cornell.edu if ( outputs[i] != i ) { 8612751Sqtt2@cornell.edu return EXIT_FAILURE; 8712751Sqtt2@cornell.edu } 8812751Sqtt2@cornell.edu } 8912751Sqtt2@cornell.edu 9012751Sqtt2@cornell.edu return EXIT_SUCCESS; 9112751Sqtt2@cornell.edu} 92