I am implementing a simple thread pool mechanism for my ubuntu server (for my multi-client anonymous chat program), and I need to make my worker threads sleep until a job (in the form of a function pointer and parameter) needs to be performed.
My current system is going out the window. I'm(the worker thread is) asking the manager if a job is available, and if there isn't sleep for 5ms. If there is, add the job to the working queue and run through the function. Wretched waste of cycles.
What I'd like to do is make a simple event-like system. I'm thinking about having a vector of mutexes (one for each worker) and have the handle to the mutex passed in as a parameter at creation. Then in my manager class (which holds and hands out jobs), whenever a thread is created, lock the mutex. When a job needs to be performed unlock a the next mutex in line, wait for it to be locked and unlocked, and relock it. However I'm wondering if there's a much better means to this end.
tldr; So my question is this. What is the most efficient, effective, and safest way to make a thread wait for a job from a managing class? Is polling a technique I should even consider (more than 1000 clients at a time), is mutex locking decent? Or are there other techniques?
See Question&Answers more detail:os