0

In C++, I want to create an algorithms with the following structure:

  • A sequential part
  • A parallel part A
  • A sequential part
  • A parallel part B
  • A sequential part

Using pthreads, I can think of two ways to solve the problem:

  1. Create N threads for part A and then destructing these threads after part A is finished. Then allocating N new threads for part B.
  2. Using the same threads for part A and part B using the various kinds of synchronization methods available.

How much overhead does it take to create new threads for solution 1 when performance matters. Should I go for solution 1 or solution 2?

Mosterd
  • 151
  • 1
  • 7

1 Answers1

1

Parallel frameworks such as OpenMP recycle threads. This is called a Thread Pool, and you can find information about those on the site. Here's one related post: Thread Pool vs Thread Spawning

If you're really concerned about performance, the best way to find out what suits your application is to try both approaches and measure them.

In general, if your processing task is expensive and the code is easier to understand if you just spawn new threads, then do that.

And just to colour the argument a little, check out this post that I answered using experimentation the other day: Why are 50 threads faster than 4?

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103