Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Pool suspend/resume feature

Closed / Accepted2835615fffffffffffffffffffffffffffffffffCreated 17 years agoUpdated 17 years ago
Descriptionplain2009-08-11 15:00:26
I think it would be nice to be able to suspend the processing of jobs by threads. That way one could turn off (suspend) job processing after a pool is created, load up the job queue with post, and then resume processing so the threads could process the job queue.  I have found on slower machines with only 2 cores that thread processing interfere with post operations. A possible solution would be to implement a tpool::pending command with a suspend/resume option that would return the list of pending jobs.
dkfplain2009-08-17 16:02:26
You could try holding a mutex in the main thread, sending a job to each of the workers which briefly grabs the mutex (i.e. which blocks until the main thread is ready). Since you know that none of the jobs will complete, you can be sure that you've got the pool fully populated and that it won't consume more until you're ready. Then load up the pool's queue with the real jobs and release the mutex from the main thread to fire.

But consider whether this is really the right approach. With only two cores, your optimal pool size is pretty small and if you need the main thread to remain responsive then perhaps your pool should only be size 1! Investing in a bigger machine may be more cost effective. (None of the advice in this paragraph is Tcl-specific; it applies to any serious system of thread pools without nasty locking problems, as it is a matter of system resourcing.)
ferrieuxplain2009-08-17 18:11:51
To amplify Donal's "is it the right approach ?" hint, also notice that if the interference with post operations is just a slowdown (rather than a race condition), this would be better handled through priorities. I have no acquaintance with thread-level priorities, but process-level ones work nicely ;-) Just lower the prio of your worker threads relatively to the launcher's and you're done.
vasiljevicplain2009-08-19 04:30:44
Hi!

I believe this is trivially possible. I could just flip a bit in the pool structure that will be examined by every pool worker.
In case bit it set, the worker would spin, otherwise it would service the pool. I could implement this with two commands: [tpool::suspend poolName] and [tpool::resume poolName]. It is also possible to achieve this "from the outside" by making a global mutex and some simple script-coding but it would be more complicated then the C-level implementation. I cannot promise when but if you need it very fast, then please submit the implementation (it is really
símple) as a diff and I will apply it. Otherwise I will do it but you will need to be patient for some time...

BTW... I still do not get the reason why you need such functionality...
ferrieuxplain2009-08-19 21:05:32
That sounds like a contradiction: if you don't get the reason why he needs it (I don't either), why accept it so quickly ?
A much more generally useful thing IMO would be to control thread prio...
hathawaplain2009-08-19 21:07:19
Zoran, thanks for your reply. I thought it would be trivial to do and handy to have.
I'm working on a visualization problem that can be broken up into about 50 equal chunks. The total jobs take about 4 hours on a slow machine and about 20 minutes on an 8-core machine using your thread package. I keep a progress bar going so that I can report "n of 50 completed". The natural and easy way of doing this is to do 50 posts (setup time is quick for my problem) and then sit in a wait loop, get the output of each chuck, print some messages, and update the progress bar. This doesn't really work since before the post loop can complete the tasks grab all the cores. As each task finishes, its output get queued, since I have not entered the wait loop yet. Eventually everything completes; however, the progress is not reported in a smooth manner.  I have solved my problem with creating a queue and posting from that until the pool is filled and when a task completes posting another job. This is just a pain. It would be a similar case for mutexes.  You end up managing the threads; instead of letting the threadpool do it for you. Thanks.

Barry
ferrieuxplain2009-08-19 21:12:42
Then clearly the stop-and-go feature is just a kludge and won't solve the general problem of your "manager thread" being unresponsive while the workers are eating all CPU. Again the only clean solution to this is _priorities_.
hathawaplain2009-08-19 21:32:03
I don't agree that a suspend/resume feature is a kludge, but a valid set of pool operations. The issue is not  my "manager thread" being unresponsive, but when it is unresponsive. Priorities, are not clean, easy to use, or easy to implement to solve my issue. That said, I do agree that priorities would be nice to have if you have a lot of time to implement them.  It probably would also be nice to vary the poolsize on the fly. Others could come up with other enhancements.
dkfplain2009-08-19 21:40:47
If you have to keep the manager thread responsive at all times, the only real fix is to make the pool size smaller than the number of execution units available to you (what that means is kind of complex as it depends on the system architectural details) so that at least one remains free to service the manager. Whether this is the best choice depends on what you view as valuable: getting all the answers sooner or presenting a progress bar.
dkfplain2009-08-19 21:41:14
BTW, this is why pool sizes are tunable.
vasiljevicplain2009-08-19 22:12:19
Hi!

I have just commited the changes for 
[tpool::suspend] and [tpool::resume].

First of all: it is really handy! I can imagine 
many ways how people would benefit from it.
I even contemplate to add this to the 
singleton thread, not just thread pools.  

Second: it is trivial to do at the C-level 
and (well) clumsy on Tcl.

Third: I said I did not understand why 
Barry needed it (specifically) but it was
no question about the usefulness of 
it in general. I was merely qurious what 
specific task he needs to address by
such constructs.

Please check out the head from CVS
and compile. I updated the doc/tpool.man
but haven't generated the nroff/html
part (please just look in the file for docs 
about those two commands). 

Note that you need to create certain 
number of worker threads upfront as they
(i.e. their count) will not be manipulated 
by the [tpool::resume] NOR [tpoo::post]
once the work gets posted to the pool.
vasiljevic Closed ticket [2835615fff]. [History diff]2009-08-21 02:38:18
Log in (anonymous is fine) to add a comment.