Original Post
I'm trying to implement a thread pool. Now I'm thinking of setting up a semaphore to have a count set to the number of threads I have. When a job is added, it will release the semaphore, so this will allow one thread to become unblocked, when a job is complete it will get blocked by this semaphore again my problem is, if more than 4 tasks get added, lets say 5, the semaphore will be released 5 times, now lets assume my threads unblock after this. They will all do an acquire and cause the semaphores count to go back to zero (as it only counts to 4), causing the last task to not complete. Is there a way i get get the count from a semaphore so i can put a check at the of my loop to release the semaphore again if more tasks exist? or any other suggestions on a work around? I've also been thinking of using a circular queue, but this doesnt allow for dynamic sized tasks lists so im hesitant to go down this path. I'm also wondering if its worth just telling the threads to go to sleep and waking them when a task is ready instead of causing them to be blocked on a semaphore, would this be more efficient?
for (;;)
{
sema.acquire
doTask
}