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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: William Chesters <williamc@d...>
Subject: Re: threads & OCamlTK
> 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).