main
test-branch
| Line | |
|---|
| 1 | #include <pthread.h>
|
|---|
| 2 | pthread_barrier_t barr;
|
|---|
| 3 |
|
|---|
| 4 | void * thread_worker(void *arg) {
|
|---|
| 5 | // do work
|
|---|
| 6 | // now make all the threads sync up
|
|---|
| 7 | int id = (int)arg;
|
|---|
| 8 | int res = pthread_barrier_wait(&barr);
|
|---|
| 9 | pthread_exit(NULL);
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | int main(void) {
|
|---|
| 14 | int nthreads = 5;
|
|---|
| 15 | pthread_t threads[nthreads];
|
|---|
| 16 | pthread_barrier_init(&barr, NULL, nthreads);
|
|---|
| 17 | for(int i=0; i<nthreads; i++) {
|
|---|
| 18 | pthread_create(&threads[i], NULL, thread_worker, (void *) i);
|
|---|
| 19 | }
|
|---|
| 20 | for(int i=0; i<nthreads; i++) {
|
|---|
| 21 | pthread_join(threads[i], NULL);
|
|---|
| 22 | }
|
|---|
| 23 | pthread_barrier_destroy(&barr);
|
|---|
| 24 | pthread_exit(NULL);
|
|---|
| 25 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.