1/*
2  * Copyright (c) 2018, Cornell University
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or
6  * without modification, are permitted provided that the following
7  * conditions are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * Neither the name of Cornell University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Authors: Tuan Ta
36  */
37
38#include <pthread.h>
39
40#include <cstdlib>
41#include <iostream>
42
43//------------------------------------------------------------------------
44// Create n threads, run them in parallel and wait for them in the master
45// thread.
46// Each child thread increments a shared variable m times
47//------------------------------------------------------------------------
48
49#define MAX_N_WORKER_THREADS 10
50
51typedef struct
52{
53    int nsteps;
54    int* shared_var;
55    pthread_mutex_t* lock;
56} ThreadArg;
57
58void* func( void* args )
59{
60    ThreadArg* my_args = ( ThreadArg* ) args;
61
62    int nsteps = my_args->nsteps;
63    int* shared_var = my_args->shared_var;
64    pthread_mutex_t* lock = my_args->lock;
65
66    for ( int i = 0; i < nsteps; ++i ) {
67        // acquire the lock
68        pthread_mutex_lock(lock);
69
70        // increment the shared_var
71        (*shared_var)++;
72
73        // release the lock
74        pthread_mutex_unlock(lock);
75    }
76
77    return nullptr;
78}
79
80int main( int argc, const char* argv[] )
81{
82    int n_worker_threads = 0;
83
84    // allocate all threads
85    pthread_t* threads = new pthread_t[MAX_N_WORKER_THREADS];
86    ThreadArg* t_args = new ThreadArg[MAX_N_WORKER_THREADS];
87
88    // variable shared among all threads
89    int shared_var = 0;
90
91    // number of steps each thread increments the shared_var
92    int nsteps = 10000;
93
94    // create a shared lock
95    pthread_mutex_t lock;
96    pthread_mutex_init(&lock, NULL);
97
98    int ret;
99
100    // try to spawn as many worker threads as possible
101    for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
102        t_args[tid].nsteps = nsteps;
103        t_args[tid].shared_var = &shared_var;
104        t_args[tid].lock = &lock;
105
106        // spawn thread
107        ret = pthread_create( threads + tid, nullptr, func, &t_args[tid] );
108
109        if (ret != 0)
110            break;
111
112        n_worker_threads++;
113    }
114
115    // sync up all threads
116    for ( int tid = 0; tid < n_worker_threads; ++tid ) {
117        pthread_join( threads[tid], nullptr );
118    }
119
120    // verify
121    bool passed = true;
122    if ( shared_var != n_worker_threads * nsteps )
123        passed = false;
124
125    // clean up
126    delete[] threads;
127    delete[] t_args;
128
129    if (!passed || n_worker_threads < 1)
130        return EXIT_FAILURE;
131
132    return EXIT_SUCCESS;
133}
134