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
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
4312751Sqtt2@cornell.edu//------------------------------------------------------------------------
4412751Sqtt2@cornell.edu// Create n threads and run them one after another
4512751Sqtt2@cornell.edu// Each child thread writes its thread id to an output array
4612751Sqtt2@cornell.edu//------------------------------------------------------------------------
4712751Sqtt2@cornell.edu
4812751Sqtt2@cornell.edu#define MAX_N_WORKER_THREADS 10
4912751Sqtt2@cornell.edu
5012751Sqtt2@cornell.edutypedef struct
5112751Sqtt2@cornell.edu{
5212751Sqtt2@cornell.edu    int tid;
5312751Sqtt2@cornell.edu    int* output;
5412751Sqtt2@cornell.edu} ThreadArg;
5512751Sqtt2@cornell.edu
5612751Sqtt2@cornell.eduvoid* func( void* args )
5712751Sqtt2@cornell.edu{
5812751Sqtt2@cornell.edu    ThreadArg* my_args = ( ThreadArg* ) args;
5912751Sqtt2@cornell.edu
6012751Sqtt2@cornell.edu    // write tid to this thread's output
6112751Sqtt2@cornell.edu    (*my_args->output) = my_args->tid;
6212751Sqtt2@cornell.edu
6312751Sqtt2@cornell.edu    return nullptr;
6412751Sqtt2@cornell.edu}
6512751Sqtt2@cornell.edu
6612751Sqtt2@cornell.eduint main( int argc, const char* argv[] )
6712751Sqtt2@cornell.edu{
6812751Sqtt2@cornell.edu    int n_worker_threads = 0;
6912751Sqtt2@cornell.edu
7012751Sqtt2@cornell.edu    // allocate all threads
7112751Sqtt2@cornell.edu    pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
7212751Sqtt2@cornell.edu    ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
7312751Sqtt2@cornell.edu
7412751Sqtt2@cornell.edu    // create an output array for all threads
7512751Sqtt2@cornell.edu    int* outputs = new int[MAX_N_WORKER_THREADS];
7612751Sqtt2@cornell.edu    int ret;
7712751Sqtt2@cornell.edu
7812751Sqtt2@cornell.edu    // try to spawn as many worker threads as possible
7912751Sqtt2@cornell.edu    for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
8012751Sqtt2@cornell.edu
8112751Sqtt2@cornell.edu        // set up thread args
8212751Sqtt2@cornell.edu        t_args[tid].tid = tid;
8312751Sqtt2@cornell.edu        t_args[tid].output = outputs + tid;
8412751Sqtt2@cornell.edu
8512751Sqtt2@cornell.edu        // spawn thread
8612751Sqtt2@cornell.edu        ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
8712751Sqtt2@cornell.edu        if (ret != 0 ) {
8812751Sqtt2@cornell.edu            break;
8912751Sqtt2@cornell.edu        }
9012751Sqtt2@cornell.edu
9112751Sqtt2@cornell.edu        n_worker_threads++;
9212751Sqtt2@cornell.edu
9312751Sqtt2@cornell.edu        // wait for the thread to join before moving on
9412751Sqtt2@cornell.edu        pthread_join( threads[tid], nullptr );
9512751Sqtt2@cornell.edu    }
9612751Sqtt2@cornell.edu
9712751Sqtt2@cornell.edu    // verify
9812751Sqtt2@cornell.edu    bool passed = true;
9912751Sqtt2@cornell.edu    for ( int i = 0; i < n_worker_threads; ++i ) {
10012751Sqtt2@cornell.edu        if ( outputs[i] != i ) {
10112751Sqtt2@cornell.edu            passed = false;
10212751Sqtt2@cornell.edu            break;
10312751Sqtt2@cornell.edu        }
10412751Sqtt2@cornell.edu    }
10512751Sqtt2@cornell.edu
10612751Sqtt2@cornell.edu    // clean up
10712751Sqtt2@cornell.edu    delete[] threads;
10812751Sqtt2@cornell.edu    delete[] t_args;
10912751Sqtt2@cornell.edu    delete[] outputs;
11012751Sqtt2@cornell.edu
11112751Sqtt2@cornell.edu    // failed if outputs are not correct or no worker thread was spawned
11212751Sqtt2@cornell.edu    if (!passed || n_worker_threads < 1)
11312751Sqtt2@cornell.edu        return EXIT_FAILURE;
11412751Sqtt2@cornell.edu
11512751Sqtt2@cornell.edu    return EXIT_SUCCESS;
11612751Sqtt2@cornell.edu}
117