First of all, it is the launch and launch of OpenMP with Visual Studio 2005, which is quite old; this requires some action, but it is described in the answer to this question .
Once this is done, it is quite simple to make this simple form of the parallelism task, if you have two methods that are truly completely independent. Please note that the qualifier; if the methods read the same data, that's fine, but if they update any state that uses a different method, or by calling any other routines that do this, then everything will be broken.
While the methods are completely independent, you can use sections for them ( tasks are actually a more modern way of OpenMP 3.0, but you probably cannot get OpenMP 3.0 support for such an old compiler); you will also see that people abuse the parallel for loops in order to achieve this, which at least has the advantage of letting you control the flow assignments, so I include this here for completeness, even if I cannot recommend it:
#include <omp.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> int f1() { int tid = omp_get_thread_num(); printf("Thread %d in function f1.\n", tid); sleep(rand()%10); return 1; } int f2() { int tid = omp_get_thread_num(); printf("Thread %d in function f2.\n", tid); sleep(rand()%10); return 2; } int main (int argc, char **argv) { int answer; int ans1, ans2; /* using sections */ #pragma omp parallel num_threads(2) shared(ans1, ans2, answer) default(none) { #pragma omp sections { #pragma omp section ans1 = f1(); #pragma omp section ans2 = f2(); } #pragma omp single answer = ans1+ans2; } printf("Answer = %d\n", answer); /* hacky appraoch, mis-using for loop */ answer = 0; #pragma omp parallel for schedule(static,1) num_threads(2) reduction(+:answer) default(none) for (int i=0; i<2; i++) { if (i==0) answer += f1(); if (i==1) answer += f2(); } printf("Answer = %d\n", answer); return 0; }
Doing this gives
$ ./sections Thread 0 in function f1. Thread 1 in function f2. Answer = 3 Thread 0 in function f1. Thread 1 in function f2. Answer = 3
source share