caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Julien Narboux <Julien.Narboux@inria.fr>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] The best way to circumvent the lack of Thread.kill ?
Date: Wed, 02 Nov 2005 14:23:33 +0100	[thread overview]
Message-ID: <1130937814.4327.36.camel@localhost.localdomain> (raw)
In-Reply-To: <43688C4C.2080606@inria.fr>

Am Mittwoch, den 02.11.2005, 10:52 +0100 schrieb Julien Narboux:
> Hi,
> 
> I just encountered the Thread.kill "not implemented" exception.

This operation is insane, better it would be removed entirely.

What could be worth of discussion is a Thread.send_exception that will
cause that a certain thread raises a certain exception the next time it
executes O'Caml code. This won't work if the thread is blocked, however.
(There is machinery to handle such asynchronous events in the O'Caml
runtime.)

For Unix (including MacOS X) you can implement send_exception yourself
by sending a signal to the thread, and defining a signal handler that
just raises an exception.

In Windows there are no signals. An extension of the O'Caml runtime
would be needed to get this behaviour.

> What is the best solution ? start a new process and use the kill at the 
> operating system level ?

Which is unavailable in Windows.

> (to make things even worse I need something which works on linux, 
> windows and macosx)

There is a hack that works (Xavier forgive):

exception User_interrupt


let do_something() =
  ignore(7 * 6)
;;


let compute() =
  try
    while true do
      do_something()
    done;
    assert false
  with
    | User_interrupt ->
	prerr_endline "Thread interrupted!"
;;


let vt_signal =
  match Sys.os_type with
    | "Win32" -> Sys.sigterm
    | _ -> Sys.sigvtalrm
;;


let interrupt = ref None;;

let force_interrupt old_action_ref n =
  (* This function is called just before the thread's timeslice ends *)
  if Some(Thread.id(Thread.self())) = !interrupt then
    raise User_interrupt;
  match !old_action_ref with
    | Sys.Signal_handle f -> f n
    | _ -> failwith "Not in threaded mode"
;;


let main() =
  (* Install the signal handler: *)
  let old_action_ref = ref Sys.Signal_ignore in
  let old_action = 
    Sys.signal vt_signal (Sys.Signal_handle (force_interrupt old_action_ref)) in
  old_action_ref := old_action;
  (* Fire up the compute thread: *)
  let t = Thread.create compute () in
  (* Wait for user: *)
  print_string "Press Return: ";
  flush stdout;
  let _ = read_line() in
  interrupt := Some (Thread.id t);
  (* Wait until the thread terminates: *)
  Thread.join t
;;

main();;

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Telefon: 06151/153855                  Telefax: 06151/997714
------------------------------------------------------------


  parent reply	other threads:[~2005-11-02 13:23 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-02  9:52 Julien Narboux
2005-11-02 10:54 ` [Caml-list] " Richard Jones
2005-11-02 11:22   ` Julien Narboux
2005-11-02 13:00     ` Jacques Garrigue
2005-11-02 12:57       ` Julien Narboux
2005-11-02 13:23 ` Gerd Stolpmann [this message]
2005-11-02 14:00   ` Gerd Stolpmann
2005-11-02 14:32   ` Julien Narboux
2005-11-02 15:07     ` Gerd Stolpmann
2005-11-02 14:53 ` David Teller
2005-11-02 16:24   ` Alessandro Baretta
2005-11-02 17:00     ` David Teller
2005-11-02 18:43       ` Alessandro Baretta
2005-11-02 18:29         ` David Teller
2005-11-08 20:36           ` Jonathan Bryant
2005-11-09  1:18             ` Grégory Guyomarc'h
2005-11-09 12:37             ` Richard Jones
     [not found]             ` <4371A0A6.4010306@laposte.net>
2005-11-09 13:32               ` Jonathan Bryant
2005-11-02 11:33 EL CHAAR Rabih   SGAM/AI/SAM
2005-11-08  3:23 ` Igor Pechtchanski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1130937814.4327.36.camel@localhost.localdomain \
    --to=info@gerd-stolpmann.de \
    --cc=Julien.Narboux@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).