Subj : thread control by mutex, predicates and conditions puzzle To : comp.programming.threads From : roland Date : Sun Jan 02 2005 04:03 pm I am seeking for help on a problem that puzzles me for a while yet. Given: Multiple threads t1, t2, ... A variable a, and N variables v1, ... vN. a is holding a state that controls global thread (t1) behaviour, while v1,...vN deal with interthread communication issues. E.g. a contains a flag requesting the thread to abort whatever it is doing and to restart. v1..vN control buffers and the like. My question now is how to protect the variables correctly by mutexes and how to assign predicates and conditions? One obvious solution to me, is to have exactly one mutex, one predicate and one condition. So the next question is: Is this the only valid solution possible? I am afraid yes, but I did not yet manage to prove this formally. Some thoughts: Assume v1 is evaluated by predicate p1, v2 by p2, .... Variable a is evaluated by predicate pa. Further p1 has condition c1, p2 has c2, ... and pa has ca. Since evaluating any of predicates p1,..pN does not reveal information about pa I need to modify my predicates as following: p1' = pa || p1 p2' = pa || p2 .... pN' = pa || pN and associated conditions c1' ... cN'. Now since a condition can be bound at most to a single mutex, when performing a wait on any of c1,...cN I am forced to use this mutex also for protection of a. If it were different in order to change state of a, an external thread would need to know on which of the c1',...cN' the thread is currently blocked. It could then lock a hypothetical mutex and change a. However this information is not available. The only remedy is to have a single mutex. There still are mutiple predicates p1',... and conditions c1',... What happens when I change the state of a? I lock the mutex, change a and then I need to signal _all_ c1', ... cN'. (Since I do not know from outside on which ci' the thread is currently blocking, if at all.) Since this is impractical from a source code maintanance perspective (would need to add a signaling function whenever a new predicate is added) I am stuck with exactly one predicate condition pair. If this result indeed is true I am asking if there is another pattern that helps me solve this kind of problem. If it is not true, and I am overlooking something important I would kindly ask to point me to where I am wrong. Thank you for your attention. Roland .