From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: shawjef3@msu.edu
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] crash under macos x but not win32
Date: Tue, 10 Apr 2007 10:07:59 +0900 (JST) [thread overview]
Message-ID: <20070410.100759.08076654.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <E1Hb45K-0007eN-K8@sys29.mail.msu.edu>
From: "Jeffrey Loren Shaw" <shawjef3@msu.edu>
> The following works as intended in Win32 (the ui counts slowly from 0 to 5),
> but crashes in Mac OS X with "Bus Error". I'm running ocaml 3.09.3 installed
> with macports. For windows I used the Ocaml 3.09.3 MinGW binary
> distribution.
>
> (* looptest.ml *)
>
> open Tk
>
> let testone () =
> let top = openTk () in
> let l = Label.create top in
> let loopfun () =
> ignore
> (
> Thread.create
> (fun () ->
> for i=0 to 5 do
> Thread.delay 1.;
> Label.configure ~text:(string_of_int i) l
> done
> )
> ()
> )
> in
> let b = Button.create ~text:"Run the test" ~command:loopfun top in
> pack [l];
> pack [b];
> mainLoop ();;
>
> testone ()
>
> (* end looptest.ml *)
>
> runs with: ocaml -I +labltk -I +threads unix.cma threads.cma labltk.cma
> looptest.ml
The Aqua version of Tk is notoriously unstable when used with labltk,
but this doesn't seem to be the problem here. Rather, it appears not
to be reentrant (or rather not callable from several threads.) By the
way I am surprised you have no problem under Win32, which often has
reentrance problems too.
Anyway, there is a Tkthread module to solve these reentrance problems.
Here is the code using it, adding "sync" and "async" where needed.
open Tk
open Tkthread
let tk = start () (* Calls openTk and mainLoop in a new thread *)
let testone () =
Thread.delay 0.1; (* wait for initialization *)
let l = sync Label.create top in
let loopfun () =
ignore
(
Thread.create
(fun () ->
for i=0 to 5 do
Thread.delay 1.;
async (Label.configure ~text:(string_of_int i)) l
done
)
()
)
in
let b = sync (Button.create ~text:"Run the test" ~command:loopfun) top in
async pack [l];
async pack [b];
Thread.join tk;; (* wait for mainLoop to finish *)
testone ()
And start it with
ocaml -I +labltk -I +threads unix.cma threads.cma labltk.cma tkthread.cmo looptest2.ml
The wait for initialization part is kind of hacky, as the thread start
function was rather intended for interactive use, but it should be
portable enough.
Jacques Garrigue
next prev parent reply other threads:[~2007-04-10 1:08 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-10 0:19 Jeffrey Loren Shaw
2007-04-10 1:07 ` Jacques Garrigue [this message]
2007-04-10 2:08 ` [Caml-list] " skaller
2007-04-10 2:41 ` Jacques Garrigue
2007-04-10 3:56 ` Jeffrey Loren Shaw
2007-04-10 4:15 ` Jacques Garrigue
2007-04-10 5:52 ` skaller
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=20070410.100759.08076654.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=shawjef3@msu.edu \
/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;
as well as URLs for NNTP newsgroup(s).