Reputation: 435
I am working on a system with 4 cores. My code can be split up into parts that each work independently, but that require a significant amount of memory.
Is it possible to make my system wait for a thread to finish before creating a new one, such that I only have 4 threads running simultaneously, while still using all my cores completely?
Upvotes: 0
Views: 944
Reputation: 5102
Use a queue where you post the requests.
Each request object with an execute method that encapsulates the work to do.
You launch the (four) threads at start up.
The threads run in a infinite loop (or until some indication is received that the thread must end), waiting for requests to come up in the queue and executing them.
Waiting can be done either by:
... in any case do not spin blindly in the loop
You need some mutex protecting the request queue... because it is memory shared by all threads.
Edit: Some pseudocode to help in understanding.
a) On initialization.
spawn 4 threads (all running mainThread())
b) On mainThread()
while(main program is running)
poll queue // shared by all threads
retrieve first Request object from queue.
invoke execute method of Request object
sleep some time (10 ms) // or use a condition/signal
end loop
As you can see the thread does not concern on the state of other threads, it just go to pick up some job, execute and come back for more.
You ensure that there's no more than 4 jobs running simultaneously at any time, because you only have 4 threads.
c) You have a Request class:
class Request {
public:
virtual ~Request() { }
virtual void execute() =0;
};
And one or more child classes:
class ThingToDoOfType1: public Request {
public:
void execute() {
// whatever means the "thing to do of type 1"
}
}
Again the Request do not concern about the state of other requests... if they need to concern... then it becomes slightly more difficult.
d) Finally you need somewhere to insert Requests of the different types in the queue. They can be inserted....
An example could be in the initialization:
spawn 4 threads (all running maintThread())
Insert 1000 Requests of Type1, Type2 and Type 3.
The 1000 requests will be added two the queue from where it will be taken each time a thread finishes processing the previous request.
Upvotes: 2
Reputation: 95
Let the main thread create a thread only when the count is less than 4. Whenever a thread is closed, this count can be decremented and if a new thread is spanned count can be incremented. Make sure that count is mutex protected.
Upvotes: 0