test_pthread_cond.cpp revision 12751
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 * Author: Moyang Wang 3612751Sqtt2@cornell.edu */ 3712751Sqtt2@cornell.edu 3812751Sqtt2@cornell.edu#include <pthread.h> 3912751Sqtt2@cornell.edu 4012751Sqtt2@cornell.edu#include <cstdlib> 4112751Sqtt2@cornell.edu#include <iostream> 4212751Sqtt2@cornell.edu#include <mutex> 4312751Sqtt2@cornell.edu#include <vector> 4412751Sqtt2@cornell.edu 4512751Sqtt2@cornell.edu//------------------------------------------------------------------------ 4612751Sqtt2@cornell.edu// Test pthread_cond 4712751Sqtt2@cornell.edu//------------------------------------------------------------------------ 4812751Sqtt2@cornell.edu// The master thread creates N threads, each of which waits on a 4912751Sqtt2@cornell.edu// condition variable of a signal to start. The master thread then set 5012751Sqtt2@cornell.edu// the signal and notifies all other threads to begin. 5112751Sqtt2@cornell.edu 5212751Sqtt2@cornell.edu#define MAX_N_WORKER_THREADS 10 5312751Sqtt2@cornell.edu 5412751Sqtt2@cornell.edupthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 5512751Sqtt2@cornell.edupthread_cond_t cv = PTHREAD_COND_INITIALIZER; 5612751Sqtt2@cornell.edu 5712751Sqtt2@cornell.edubool ready = false; 5812751Sqtt2@cornell.edu 5912751Sqtt2@cornell.eduvoid* print_id( void* arg_vptr ) 6012751Sqtt2@cornell.edu{ 6112751Sqtt2@cornell.edu pthread_mutex_lock( &mutex ); 6212751Sqtt2@cornell.edu long id = (long)arg_vptr; 6312751Sqtt2@cornell.edu 6412751Sqtt2@cornell.edu while (!ready) { 6512751Sqtt2@cornell.edu pthread_cond_wait( &cv, &mutex ); 6612751Sqtt2@cornell.edu } 6712751Sqtt2@cornell.edu // ... 6812751Sqtt2@cornell.edu std::cout << "thread " << id << '\n'; 6912751Sqtt2@cornell.edu 7012751Sqtt2@cornell.edu pthread_mutex_unlock( &mutex ); 7112751Sqtt2@cornell.edu 7212751Sqtt2@cornell.edu return nullptr; 7312751Sqtt2@cornell.edu} 7412751Sqtt2@cornell.edu 7512751Sqtt2@cornell.eduvoid go() 7612751Sqtt2@cornell.edu{ 7712751Sqtt2@cornell.edu pthread_mutex_lock( &mutex ); 7812751Sqtt2@cornell.edu ready = true; 7912751Sqtt2@cornell.edu pthread_cond_broadcast( &cv ); 8012751Sqtt2@cornell.edu pthread_mutex_unlock( &mutex ); 8112751Sqtt2@cornell.edu} 8212751Sqtt2@cornell.edu 8312751Sqtt2@cornell.eduint main( int argc, char* argv[] ) 8412751Sqtt2@cornell.edu{ 8512751Sqtt2@cornell.edu size_t n_worker_threads = 0; 8612751Sqtt2@cornell.edu 8712751Sqtt2@cornell.edu std::vector< pthread_t > threads( MAX_N_WORKER_THREADS ); 8812751Sqtt2@cornell.edu 8912751Sqtt2@cornell.edu int ret = 0; 9012751Sqtt2@cornell.edu for ( size_t i = 0; i < MAX_N_WORKER_THREADS; i++ ){ 9112751Sqtt2@cornell.edu ret = pthread_create( &threads[i], nullptr, print_id, (void*)i ); 9212751Sqtt2@cornell.edu if (ret != 0) { 9312751Sqtt2@cornell.edu break; 9412751Sqtt2@cornell.edu } 9512751Sqtt2@cornell.edu n_worker_threads++; 9612751Sqtt2@cornell.edu } 9712751Sqtt2@cornell.edu 9812751Sqtt2@cornell.edu std::cout << n_worker_threads << " threads ready to race...\n"; 9912751Sqtt2@cornell.edu 10012751Sqtt2@cornell.edu go(); 10112751Sqtt2@cornell.edu 10212751Sqtt2@cornell.edu for ( size_t i = 1; i < n_worker_threads; i++ ) { 10312751Sqtt2@cornell.edu pthread_join( threads[i], nullptr ); 10412751Sqtt2@cornell.edu } 10512751Sqtt2@cornell.edu 10612751Sqtt2@cornell.edu if (n_worker_threads < 1) { 10712751Sqtt2@cornell.edu return EXIT_FAILURE; 10812751Sqtt2@cornell.edu } 10912751Sqtt2@cornell.edu 11012751Sqtt2@cornell.edu return EXIT_SUCCESS; 11112751Sqtt2@cornell.edu} 112