edcb046ca5dc3dba79a77e3aef96eab71e669a95
2 * Copyright (c) 2018, Cornell University
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
44 //------------------------------------------------------------------------
45 // Create n threads, run them in parallel and wait for them in the master
47 // Each child thread increments a shared variable m times atomically
48 //------------------------------------------------------------------------
50 #define MAX_N_WORKER_THREADS 10
55 std::atomic
<int>* shared_var
;
58 void* func( void* args
)
60 ThreadArg
* my_args
= ( ThreadArg
* ) args
;
62 int nsteps
= my_args
->nsteps
;
63 std::atomic
<int>* shared_var
= my_args
->shared_var
;
65 for ( int i
= 0; i
< nsteps
; ++i
) {
66 std::atomic_fetch_add(shared_var
, 1);
72 int main( int argc
, const char* argv
[] )
74 int n_worker_threads
= 0;
76 // allocate all threads
77 pthread_t
* threads
= new pthread_t
[MAX_N_WORKER_THREADS
];
79 // variable shared among all threads
80 std::atomic
<int> shared_var(0);
82 // number of steps each thread increments the shared_var
85 // set up threads' arguments
86 ThreadArg
* t_args
= new ThreadArg
[MAX_N_WORKER_THREADS
];
89 for ( size_t tid
= 0; tid
< MAX_N_WORKER_THREADS
; tid
++ ){
90 t_args
[tid
].nsteps
= nsteps
;
91 t_args
[tid
].shared_var
= &shared_var
;
94 ret
= pthread_create( threads
+ tid
, nullptr, func
, &t_args
[tid
] );
103 // sync up all threads
104 for ( int tid
= 0; tid
< n_worker_threads
; ++tid
) {
105 pthread_join( threads
[tid
], nullptr );
113 if ( shared_var
!= n_worker_threads
* nsteps
|| n_worker_threads
< 1)