Created
April 13, 2016 07:30
-
-
Save yohm/14971a48a0250ffac350899552906a8f to your computer and use it in GitHub Desktop.
testing coexistence of pthread and OpenMP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <omp.h> | |
#define NTHREADS 4 | |
void *myFun(void *x) | |
{ | |
int tid; | |
tid = *((int *) x); | |
printf("Hi from thread %d!\n", tid); | |
#pragma omp parallel | |
{ | |
int n = omp_get_num_threads(); | |
#pragma omp master | |
printf("num threads: %d\n", n); | |
#pragma omp for | |
for( int i=0; i < n; i++) { | |
int t = omp_get_thread_num(); | |
printf("I'm thread %d-%d!\n", tid, t ); | |
} | |
} | |
return NULL; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
pthread_t threads[NTHREADS]; | |
int thread_args[NTHREADS]; | |
int rc, i; | |
/* spawn the threads */ | |
for (i=0; i<NTHREADS; ++i) | |
{ | |
thread_args[i] = i; | |
printf("spawning thread %d\n", i); | |
rc = pthread_create(&threads[i], NULL, myFun, (void *) &thread_args[i]); | |
} | |
/* wait for threads to finish */ | |
for (i=0; i<NTHREADS; ++i) { | |
rc = pthread_join(threads[i], NULL); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing Environment: MacOSX ElCapitan
When I use g++-4.9 installed via homebrew, I got SEGV as follows.
However, if I use g++-5, pthread and OpenMP coexists well.