Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Future of labels
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Suspending threads
>   I'm trying to suspend a thread and then
> have another thread wake it up. However, 
> I can't find a suspend (nor a dual wake-up) function in 
> the Threads library. 
>   Any help? 

Right.  The reason there are no "suspend" and "resume" operations on
threads is that not all system thread libraries provide it, most
notably Posix threads.  There are very good reasons not to give these
operations to users, because they are extremely error-prone and make
little sense from a concurrency point of view: stopping a thread at
any time without its consent, e.g. while it is holding a mutex on
critical data, can deadlock the whole program.

The only legitimate uses for suspend/resume are 1- for low-level
services such as garbage collection and debuggers, and 2- to create a
thread in the suspended state, finish initialization, then let it
run.  But Posix decided these uses were not worth supporting :-)

As someone else replied, you should use a mutex / boolean reference /
condition variable combination so that the thread can voluntarily
suspend itself on the condition, and allow other threads to restart it
by signalling the condition.

The mutex / condition variable / predicate on shared state model
inherited from Posix may look heavy and restrictive at first sight,
but is actually quite flexible and less error-prone than alternatives.

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr