Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Inter-thread exceptions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-05 (11:31)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Inter-thread exceptions
> Hello.
> How big a task would it be to implement asynchronous inter-thread
> exceptions? In practice the feature would amount to a function:
> val raise_to : exn -> Thread.t -> unit
> This would be most handy for implementing eg. timeouts: just make a thread
> that sleeps and raises an exception in the other thread when the time is
> spent.

Yes, this is a reasonable model for thread cancellation -- a lot better
than Thread.kill, because the "victim" thread can catch the exception
and perform cleanup actions like releasing mutexes.  

It's not hard to implement with bytecode-level threads (where Caml
does the scheduling), but there is a major difficulty with system
threads: if the target thread is blocked on a system call such as
network I/O, it is extremely difficult to terminate the system call
prematurely and have the target thread honor the exception immediately.

(Under Unix, signals can allow this to some extent, but the semantics
of signals in POSIX threads prevents directing a signal to a
particular thread.  POSIX thread cancellation could perhaps be abused
to deal with this situation, however.  Under Windows, the situation is
even worse...)

So, the only easily implementable behavior is that the target thread
of a "raise_to" operation can delay the processing of the exception
until it returns from a system call.  But this behavior is nearly

- Xavier Leroy
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: