/* example1: fork parallel threads; each thread prints hello; when all threads are done, the master thread prints how many threads are in total. */ #include #include #include int main (int argc, char *argv[]) { int th_id, nthreads; #pragma omp parallel private(th_id) /* the block below is executed in parallel by as many threads as cores; th_id is declared above and is global to all threads, however is is specified as private; so each thread will have its own copy of th_id */ { th_id = omp_get_thread_num(); printf("Hello World from thread %d\n", th_id); //#pragma omp barrier /* a barrier means that all threads wait until all teh otehr threads have reached this point */ if ( th_id == 0 ) { nthreads = omp_get_num_threads(); printf("There are %d threads\n",nthreads); } } // get_num_threads() is 1 outside a parallel section nthreads = omp_get_num_threads(); printf("\n\nThere are %d threads\n",nthreads); return EXIT_SUCCESS; }