From: Don Syme <dsyme@microsoft.com>
To: "'William Chesters'" <williamc@dai.ed.ac.uk>, caml-list@inria.fr
Subject: RE: threads & OCamlTK
Date: Wed, 17 Mar 1999 07:02:45 -0800 [thread overview]
Message-ID: <39ADCF833E74D111A2D700805F1951EF0F00BA7A@RED-MSG-06> (raw)
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;;
next reply other threads:[~1999-03-17 21:03 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-03-17 15:02 Don Syme [this message]
1999-03-17 15:57 ` William Chesters
-- strict thread matches above, loose matches on Subject: below --
1999-03-16 17:32 William Chesters
1999-03-15 15:05 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
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=39ADCF833E74D111A2D700805F1951EF0F00BA7A@RED-MSG-06 \
--to=dsyme@microsoft.com \
--cc=caml-list@inria.fr \
--cc=williamc@dai.ed.ac.uk \
/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