quant
quant

Reputation: 23052

How do I explicitely thread tasks?

Suppose I have two functions:

Fun1();
Fun2();

These functions are independent and the task would be improved if I could run them in parallel (that is, run Fun1 on one thread and Fun2 on another. I am using Visual Studio 2012 - so Open MP 2.0.

Is there a straight-forward way (without any parallel region thread number testing or for loop dodginess) to achieve this? Does OpenMP provide this kind of functionality?

I tried looking at the parallel and task directives as a place to start but I find most of the literature thoroughly incomprehensible and couldn't find any examples...

Upvotes: 0

Views: 98

Answers (3)

Massimiliano
Massimiliano

Reputation: 8032

You may use a sections work-sharing construct:

#pragma omp parallel sections
{
#pragma omp section
  Fun1();
#pragma omp section
  Fun2();
}

Quoting from OpenMP 2.0 specifications (sec. 2.4.2):

The sections directive identifies a noniterative work-sharing construct that specifies a set of constructs that are to be divided among threads in a team. Each section is executed once by a thread in the team.

Upvotes: 4

MasterAler
MasterAler

Reputation: 1653

Yeah, why not using native threads or some wrappers around them? Open MP, as far as i know is much more powerful, than a simple thread runner tool. Of course, you can mark some flag variable as shared or private and check it inside parallelling pragma or perhaps obtain appropriate results with using schedule directive (don't remember exactly all OMP features), but...

  • You've got only couple of tasks
  • It's not really the parallel computing, it's not eveb some kind of primitive thread pool, just the same problem as that of getting GUI and calculations in 2 single threads
  • You haven't told anything about synchronization or complex thread cooperation

Looks like u don't really need Open MP.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

C++11:

std::thread thread1(Fun1);
Fun2(); // runs on main thread
thread1.join(); // wait for completion

If you don't have C++11, you can replace std::thread with boost::thread and do the same.

Upvotes: 5

Related Questions