English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] Are unreachable threads garbage collected?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-12-04 (02:29)
From: osiire <osiire@k...>
Subject: Re: [Caml-list] Are unreachable threads garbage collected?

On Sat, 3 Dec 2005 16:16:29 -0800 (PST)
In <20051204001629.39621.qmail@web34610.mail.mud.yahoo.com>
Ker Lutyn <ker527mail@yahoo.com> wrote:
> Here's my reworking of this example to make the thread go away if the buffered
> channel becomes "unreachable". The strategy is to allocate the channel object
> at the very end and associate a Gc.finalise function with it that syncs an
> event on a kill channel with the thread.

> Does this look like a reasonable design pattern for this?

I think the strategy is reasonable. But the pattern can be more simple.

let make_gc_event v =
  let gc_channel = new_channel () in
  let gc_send () = sync (send gc_channel ()) in
  let finalise _ = ignore (Thread.create gc_send ()) in
  Gc.finalise finalise v;
  wrap (receive gc_channel)

usage is as follow:

  let bc = i, o in
  let gc = make_gc_event bc in
  let rec loop q =
    select [
      wrap (receive i) (fun x -> let ... in loop q);
      wrap (send o v) (fun _ -> loop q);
      gc (fun () -> Printf.printf "killed\n"; flush stdout; ())
  ignore (Thread.create loop q);