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: Ker Lutyn <ker527mail@y...>
Subject: Re: [Caml-list] Are unreachable threads garbage collected?
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?

module Buffered_channel = struct
  open Event
  type 'a t = 'a channel * 'a channel
  let make () =
    let i = new_channel () in
    let o = new_channel () in
    let rec queue_event = function
      | [], [] ->
	  wrap (receive i) (fun x -> Some ([x], []))
      | (a :: aa) as aaa, bbb -> choose [
	  wrap (receive i) (fun x -> Some (aaa, x :: bbb));
	  wrap (send o a) (fun () -> Some (aa, bbb))
	]
      | [], bbb -> queue_event (List.rev bbb, [])
    in
    let kill_channel = new_channel () in
    let kill_event =
      wrap (receive kill_channel) (fun () -> None)
    in
    let rec loop q =
      match select [kill_event; queue_event q] with
	| None -> ()
	| Some q -> loop q
    in 
    ignore (Thread.create loop ([], []));

    let kill_send () = sync (send kill_channel ()) in
    let kill_function _ = ignore (Thread.create kill_send ()) in
    let bc = i, o in
    Gc.finalise kill_function bc;
    bc
  let send (i, _) x = Event.send i x
  let receive (_, o) = Event.receive o
end

--- Jonathan Roewen <jonathan.roewen@gmail.com> wrote:

> The channel won't become unreachable: The infinite loop as the thread
> function keeps it alive. Only way for any of it to be GC'd is if one
> of the functions for wrap throws an exception that propogates out.
> 
> Jonathan
> 
> On 12/4/05, Ker Lutyn <ker527mail@yahoo.com> wrote:
> > >From Reppy's paper, an implementation of buffered channels translated to
> OCaml.
> > Note the internal thread to manage the state. If the buffered channel
> becomes
> > unreachable, will this thread be garbage collected?
> >
> > module Buffered_channel = struct
> >  open Event
> >  type 'a t = 'a channel * 'a channel
> >  let make () =
> >    let i = new_channel () in
> >    let o = new_channel () in
> >    let rec loop = function
> >      | [], [] -> loop ([sync (receive i)], [])
> >      | (a :: aa) as aaa, bbb -> select [
> >          wrap (receive i) (fun x -> loop (aaa, x :: bbb));
> >          wrap (send o a) (fun () -> loop (aa, bbb))
> >        ]
> >      | [], bbb -> loop (List.rev bbb, [])
> >    in ignore (Thread.create loop ([], [])); (i, o)
> >  let send (i, _) x = Event.send i x
> >  let receive (_, o) = Event.receive o
> > end
> >
> >
> >
> >
> > __________________________________
> > Start your day with Yahoo! - Make it your home page!
> > http://www.yahoo.com/r/hs
> >
> > _______________________________________________
> > Caml-list mailing list. Subscription management:
> > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> > Archives: http://caml.inria.fr
> > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> > Bug reports: http://caml.inria.fr/bin/caml-bugs
> >
> 



		
__________________________________ 
Start your day with Yahoo! - Make it your home page! 
http://www.yahoo.com/r/hs