C/C++ implementation of multithreaded boss/workers model
I am looking for a C/C++ implementation of a multi threaded job dispatcher. Boss thread (the main function) reads the job (eg executing msgrcv in a while(1) loop).
The boss read开发者_如何学JAVAs the new job and depending on the destination dispatches the job to the corresponding thread. The worker thread executes the job.
I don't have the time to build it from scratch, I am looking a ready solution.
UPDATE: I cannot use boost. It must be in pthreads (Linux)
Your simplest option is to have the worker threads waiting on a condition variable which gets signalled when a job comes in for them to read.
You would need a mutex that gets locked whilst you check the condition. When you wait on the condition variable it automatically unlocks the associated mutex until the condition gets signalled at which point your thread will have it. You should check your condition again.
You could have one condition variable for each queue or one for all the threads, it depends on how you are scaled.
Sound like you're after a thread pool. One can be found here (it requires boost). I'm sure Googling for "thread pool" or something similar will yield other posibilities.
精彩评论