Scheduling huge number of threads so only 4 are executed in parallel

As already stated in the title I have a large number of threads (probably much higher than 100) that are rather saving a program state than running. I want only few of them (enough to use all physical processors) to really run concurrent and the rest should wait until one of the running is blocked. When this happens a new one should be running.

Is it possible to achieve this with pthreads for example with the pthread scheduling functions? How would you do this?

Regards,

Nobody

EDIT More Information: Each thread fetches a job from the taskpool on its own and goes on to a certain point. I need 100 threads to gather at that certain point of program execution that cannot be calculated in parallel. When the calculation is done the threads should be awakened and go on. To make this efficient I have to avoid the scheduler from wasting time on switching between 100 threads instead of 4.

Upvotes: 0

Views: 200

Answers (4)

dbeer
dbeer

Reputation: 7203

I don't know how to do this with pthread functions, but I do have an idea:

I would implement this by adding some intelligence to the threadpool/taskpool to count the number of active threads and only make 4 - number of active threads available at any one time. This could be done by having an idle queue, a ready queue, and an active queue (or just active count). Tasks would grab from the ready queue, and the threadpool would only migrate tasks from the idle queue to the ready queue conditionally.

Upvotes: 0

Dennis Ruck
Dennis Ruck

Reputation: 1

  • Initialize a global variable to the number of threads to run concurrently.
  • When a thread wants to do work it obtains a slot. Using a mutex and condition variable, it waits until slots_available > 1. It then decrements slots_available releases the mutex and proceeds with its work.
  • When a thread has completed its work, it releases the slot by locking the mutex and incrementing slots_available. It signals all threads waiting on the condition variable so they can wake and see if slots_available > 1.
  • See https://computing.llnl.gov/tutorials/pthreads/#Mutexes for specific pthread library calls to use for the above.

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283684

Just use a semaphore with initial count of 4?

Upvotes: 2

drb
drb

Reputation: 738

You could always launch 4 at a time, assigning them to a thread group, then waiting with a join all on the thread group. But I think more information is needed to develop a really useful answer.

Upvotes: 0

Related Questions