Pthreads vs. OpenMP

Viewed 58841

I'm creating a multi-threaded application in C using Linux.

I'm unsure whether I should use the POSIX thread API or the OpenMP API.

What are the pros & cons of using either?

Edit:

Could someone clarify whether both APIs create kernel-level or user-level threads?

5 Answers

Think of it this way. On a linux system, it is very highly likely that the OpenMP API itself uses pthreads to implement its features such as parallelism, barriers and locks/mutex. Having said that, there are good reasons to work directly with the pthreads API.

It is my opinion that -

You use OpenMP when -

  • Your program contains easy to spot for loops where each iteration is independent from other.
  • You want to retain the readability of the program. (Basically keep the parallelized program looking like the sequential version)
  • You want to stay away from the nitty-gritty details of how threads are spawned and controlled at a micro level.

And pthreads when -

  • You don't have easy to parallelize loops.

  • You have different tasks which need to be performed concurrently, you might be wanting to give different responsibilities to each of those tasks.

  • You want to control the flow of thread execution at a micro level.

Feel free to correct me in the comments.

Related