Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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; ())
    ]
  in
  ignore (Thread.create loop q);
  bc

----
 osiire