0

I have two function which are not related to each other for example:

int add(int num)
{ 
     int sum=0;
     for(i=0;i<num;++i)
         sum+=i;
    return sum;
}

int mul(int num)
{  
    int mul=1;
    for(int i=1;i<num;++i)
       mul * i;
    return mul;
}

and I am suing them as follow:

auto x=add(100);
auto m=mul(200);
cout<<a<< "    " <<m<<endl;

How can I run them in parallel using OpenMP? I know that I can run them in parallel if I create a new thread and run one of the functions in that thread and implement a sync mechanizim to make sure that both threads finisshes by time that cout is called.

Also I know that I can use openMP parallel for for my loops, but assume that it is not there.

3
  • @VladimirF Do you mean similar to the code presented at the answer to this question: stackoverflow.com/questions/2770911/… Commented May 11, 2015 at 15:43
  • yes, something like that Commented May 11, 2015 at 15:47
  • @VladimirF Pleae put your comment in an answer so I can accept it. Commented May 15, 2015 at 15:48

1 Answer 1

1

The usual way to sort out this problem in OpenMP is the sections construct. It enables you to define parts of your sequential code, that can be computed concurrently by different threads. Each section starts with the omp section directive/pragma.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.