caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* threads & OCamlTK
@ 1999-03-15 15:05 Don Syme
  1999-03-16 17:18 ` Francois Rouaix
  1999-03-22 18:39 ` Trevor Jim
  0 siblings, 2 replies; 11+ messages in thread
From: Don Syme @ 1999-03-15 15:05 UTC (permalink / raw)
  To: caml-list


And, on a vaguely related topic, is it possible to use threads with programs
that also use OCamlTK?  I know Tk is not threadsafe, but if only one thread
is calling Tk functions, then perhaps it's still OK?  

Also, the manual says:
  > All object files on the command line must also have been compiled with
the -thread option, 
  > which selects a special, thread-safe version of the standard library
(see chapter 8). 

But does this apply to the object files contained in libraries such as
OCamlTK, Unix, Num and Str?  

Finally, if a particular library has used non-threadsafe constructs, and you
try to link it into a threaded program, some compiler support for detecting
and warning about this might be handy.

Thanks,
Don


-----Original Message-----
From: Don Syme [mailto:dsyme@microsoft.com]
Sent: 15 March 1999 14:27
To: caml-list@inria.fr
Subject: Break under Windows NT




Hi,

Is it true that under the Windows NT version of OCaml there's no easy way of
sending the equivalent of a SIGINT to a OCaml process and have OCaml raise
an exception?  Or have I missed something?  I need to be able to
asynchronously break long running computations under NT.  BTW I'm also using
OCamlTK.

Thanks,
Don




^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: threads & OCamlTK
@ 1999-03-16 17:32 William Chesters
  0 siblings, 0 replies; 11+ messages in thread
From: William Chesters @ 1999-03-16 17:32 UTC (permalink / raw)
  To: caml-list

> And, on a vaguely related topic, is it possible to use threads with
> programs that also use OCamlTK? I know Tk is not threadsafe, but if
> only one thread is calling Tk functions, then perhaps it's still OK?

   No, `Tk.mainLoop' blocks the whole process.

   To get around this, I have a little module that forks and uses
marshalling to communicate GUI code you want executed to the child
process which is running `Tk.mainLoop'.  This can be done in a
type-safe way:

	let tkDo =
	  TkServer.fork (fun topLevel ->
	    Canvas.create topLevel 0 0 400 400, ref [])
        in
	...
	tkDo (fun canvas, representation ->
	  Canvas.delete !representation;
	  representation := Canvas.create_text canvas ...)

It is not too inconvenient as long as you don't worry too much about
efficiency (I think my program ends up sending rather big marshalled
closures across the IPC).




^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: threads & OCamlTK
@ 1999-03-17 15:02 Don Syme
  1999-03-17 15:57 ` William Chesters
  0 siblings, 1 reply; 11+ messages in thread
From: Don Syme @ 1999-03-17 15:02 UTC (permalink / raw)
  To: 'William Chesters', caml-list


Thanks to everyone who replied!  I'll take a good look at ThreadTk shortly.

William Chesters writes:

> > And, on a vaguely related topic, is it possible to use threads with
> > programs that also use OCamlTK? I know Tk is not threadsafe, but if
> > only one thread is calling Tk functions, then perhaps it's still OK?

>   No, `Tk.mainLoop' blocks the whole process.

Hmm... it seems not to, under Windows NT/bytecode.  Maybe this is because
Tk.mainLoop gets linked against a non-blocking library of code under
Windows.

Here's an example program where I used polling to kill looping computations.

Cheers & thanks,
Don

---------------------------------------------------------------

let loopy = ref [];;

let mk_loopy_thread id = 
  let control = ref false in
  let kill () = control := true in
  let rec loop n = 
    Format.printf "loopy thread %d computes %d@." id n;
    if !control then Thread.exit() else loop (n+1) in
  let t = Thread.create loop 0 in
  kill;;

let tk_thread = 
  Thread.create (fun () ->
    let top = openTk ()  in
  (* The widgets. They all have "top" as parent widget. *)
    let en1 = Entry.create top [TextWidth 6; Relief Sunken] in
    let lab1 = Label.create top [Text "plus"] in
    let en2 = Entry.create top [TextWidth 6 ; Relief Sunken] in
    let lab2 = Label.create top [Text "="] in
    let but1 = Button.create top [Text "loopy thread"] in
    let but2 = Button.create top [Text "kill loopy thread"] in
    let result_display = Label.create top [] in
  (* References holding values of entry widgets *)
    let n1 = ref 0
    and n2 = ref 0  in
  (* Refresh result *)
    let refresh () =
      Label.configure result_display [Text (string_of_int (!n1 + !n2))]  in
  (* Electric *)
    let get_and_refresh (w,r) =
      fun _ _ ->
	try
	  r := int_of_string (Entry.get w);
	  let _ = refresh () in
	  ()
	with
      	  Failure "int_of_string" ->
            Label.configure result_display [Text "error"]
    in
  (* Set the callbacks *)
    Entry.configure en1 [XScrollCommand (get_and_refresh (en1,n1)) ];
    Entry.configure en2 [XScrollCommand (get_and_refresh (en2,n2)) ];
    let loopy_command _ = 
      loopy := (mk_loopy_thread (List.length !loopy))::(!loopy) in
    let kill_loopy_command _ = 
      (List.hd !loopy) ();
      loopy := List.tl !loopy in
    Button.configure but1 [Command loopy_command ];
    Button.configure but2 [Command kill_loopy_command ];
  (* Map the widgets *)
    pack [en1;lab1;en2;lab2;result_display;but1;but2;] [];
  (* Make the window resizable *)
    Wm.minsize_set top 1 1;
  (* Start interaction (event-driven program) *)
    mainLoop ()) ();;

Thread.join tk_thread;; 




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~1999-03-25 13:09 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-15 15:05 threads & OCamlTK Don Syme
1999-03-16 17:18 ` Francois Rouaix
1999-03-16 17:42   ` Jun P. Furuse
1999-03-22 18:39 ` Trevor Jim
1999-03-22 20:04   ` William Chesters
1999-03-23 16:08     ` Xavier Leroy
1999-03-23 15:07   ` Jerome Vouillon
1999-03-24 17:27     ` William Chesters
1999-03-16 17:32 William Chesters
1999-03-17 15:02 Don Syme
1999-03-17 15:57 ` William Chesters

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).