ordered threading

Tim Peters tim_one at email.msn.com
Sat Jul 3 14:38:05 EDT 1999


[Robin Becker]
> I have an unbounded number of worker threads which occasionally require
> access to a unique server thread. I can easily mutex access to the
> server thread, but is there any obvious/easy way to ensure that the
> blocked workers wake up in the right order?

No threading system on earth allows specifying the order in which blocked
threads will wake up, although some systems (not Python's) allow you to
*influence* the order via assorted "priority" gimmicks.

You could easily enough build an OrderedLock class whose acquire and release
methods implement whatever scheme you want, hiding the unordered nature of
an internal mutex under a layer of serial-number cruft.  But then why not do
these things serially in a single thread to begin with?

If you have frequent rigid ordering requirements, threads are likely the
wrong approach.

> Also if blocked can a thread be killed in any way?

No, not short of killing the whole program.  A thread blocked on a condition
that will never become true is a symptom of confused application design, not
of a weakness in the thread model <wink>.

expand-the-condition-to-include-"or-i-should-die-now"-ly y'rs  - tim






More information about the Python-list mailing list