Version française
Home     About     Download     Resources     Contact us    
Browse thread
threads & OCamlTK
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Trevor Jim <tjim@s...>
Subject: Re: threads & OCamlTK
I've been using camltk with threads for a while.  I find it is faster
to run Tk as its own process -- the thread scheduler doesn't seem to
do a good job with Tk involved.

But, if you are running Windows this may not be possible.  The Tk
process will need to communicate with the other processes; the most
convenient way to do this is with sockets; and to the best of my
knowledge in Windows it isn't possible to use sockets unless you are
actually plugged in to the network.  So, if you want to run on your
Windows laptop, you are stuck with threads.

My program uses one thread to run a Tk gui.  I have the gui
periodically poll for input from the other threads.  I use channels
and events to do this.  Here's a useful function:

  let install_poll(poll_function) =
    let rec install () =
      Timer.add 1 (* Do this every 1 millisecond *)
        (fun () ->
          (match poll (receive guichannel) with
            None -> ()
          | Some le -> poll_function [le]);
          install()  (* After every poll we have to reinstall *)
    in install()

A main thread creates guichannel and all the other threads.  The Tk
thread invokes install_poll on a polling function of its choice,
taking input from guichannel.  The other threads write to guichannel.
This assumes "open Event".

BTW this doesn't work with bytecode threads.  Windows uses native
threads but for Unix systems you may have to install ocaml with a flag
to get native threads.  Under Solaris, it works poorly even with
native threads.  So, I also use a variant of install_poll to read from
a socket instead of a channel.  It's useful when I plug my laptop in
to the network, or when I'm running Unix and I can run the Tk part in
its own process.

Anyone know a better way?