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, Moyang Wang
36  */
37
38#include <cstdlib>
39#include <iostream>
40#include <mutex>
41#include <thread>
42#include <vector>
43
44//------------------------------------------------------------------------
45// Create n threads, run them in parallel and wait for them in the master
46// thread.
47// Each child thread increments a shared variable m times
48//------------------------------------------------------------------------
49
50#define MAX_N_WORKER_THREADS 10
51
52int main( int argc, const char* argv[] )
53{
54    int n_worker_threads = 0;
55
56    // allocate all threads
57    std::vector< std::thread > threads;
58
59    // mutex to protect the shared variable
60    std::mutex my_mutex;
61
62    // variable shared among all threads
63    int shared_var = 0;
64
65    // number of steps each thread increments the shared_var
66    int nsteps = 1000;
67
68    for ( int tid = 0; tid < MAX_N_WORKER_THREADS; ++tid ) {
69        try {
70            threads.push_back( std::thread( [&] {
71                        std::lock_guard<std::mutex> guard(my_mutex);
72                        for ( int i = 0; i < nsteps; ++i )
73                            shared_var++;
74                    } ) );
75        } catch ( const std::system_error& err ) {
76            break;
77        }
78        n_worker_threads++;
79    }
80
81    // sync up all threads
82    for (int i = 0; i < n_worker_threads; ++i) {
83        threads[i].join();
84    }
85
86    // verify
87    if ( shared_var != n_worker_threads * nsteps || n_worker_threads < 1) {
88        return EXIT_FAILURE;
89    }
90
91    return EXIT_SUCCESS;
92}
93