HOW TO: write multithreaded extensions for Tcl

1 view
Skip to first unread message

David Gravereaux

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
For a detailed explanation how I sync my current multithreaded
extension to Tcl's singlethreaded core, please see:

http://www.maui.net/~davygrvy/tomahawk/irc_engine_oline.html

I hope that everyone can benefit from this "trick" and that it becomes
an accepted practice.

* David Gravereaux *
Tomahawk Software Group
Mahalo Aloha from Maui, Hawaii

[[Send Tcl/Tk announcements to tcl-an...@mitchell.org
Send administrivia to tcl-announ...@mitchell.org
Announcements archived at http://www.findmail.com/list/tcl_announce/
The primary Tcl/Tk archive is ftp://ftp.neosoft.com/pub/tcl/ ]]


David Gravereaux

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to

Alexandre Ferrieux

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
David Gravereaux wrote:
>
> For a detailed explanation how I sync my current multithreaded
> extension to Tcl's singlethreaded core, please see:
>
> http://www.maui.net/~davygrvy/tomahawk/irc_engine_oline.html
>
> I hope that everyone can benefit from this "trick" and that it becomes
> an accepted practice.

David, don't forget to mention the vwait-awaking problem and the
solution you found. This too is an important thing that should become an
accepted practice.

-Alex

William H. Duquette

unread,
Mar 11, 1999, 3:00:00 AM3/11/99
to
On Wed, 10 Mar 1999 01:23:49 GMT, davy...@bigfoot.com (David
Gravereaux) wrote:

>For a detailed explanation how I sync my current multithreaded
>extension to Tcl's singlethreaded core, please see:
>
>http://www.maui.net/~davygrvy/tomahawk/irc_engine_oline.html
>
>I hope that everyone can benefit from this "trick" and that it becomes
>an accepted practice.

David's page explains how he uses Tcl_AsyncMark() to multiplex
events from other threads into a single-threaded Tcl interpreter.
It's an interesting way to do it, because it should work
on all platforms.

What I've done in the past (on Unix, only!) is to create a pipe.
Then I use Tcl_CreateFileHandler() to listen for messages
written to the pipe. In my other threads, whenever I want
to tell the main thread something, I write a message to
the pipe.

I don't know whether there's an equivalent mechanism on
Windows or not.


--------------------------------------------------------------------------
Will Duquette, JPL | William.H...@jpl.nasa.gov
But I speak only | http://eis.jpl.nasa.gov/~will (JPL Use Only)
for myself. | It's amazing what you can do with the right tools.

Alexandre Ferrieux

unread,
Mar 12, 1999, 3:00:00 AM3/12/99
to
William H. Duquette wrote:
>
> What I've done in the past (on Unix, only!) is to create a pipe.
> Then I use Tcl_CreateFileHandler() to listen for messages
> written to the pipe. In my other threads, whenever I want
> to tell the main thread something, I write a message to
> the pipe.

Yes, it's a good idea to mention this method.
A further interesting feature of it, is that you can use the pipe for
only a slight fraction of the real bandwidth, since you can pass
pointers (the threads sharing a single address space). This should get
rid of any shyness due to a would-be overwhelming I/O cost...

So, to sum it up:

" Attention multi-threaded extension writers: the two top methods to
send events to a single-threaded Tcl core (this also includes the Tk
thread of Tcl8.1) are:

- David's AsyncMark/QueueEvent/PostMessage(WM_TIMER) (Win ONLY)

- William's interthread pipe (Unix ONLY)

To this, I'd timidly add a third one, actually a variant of William's
one:

- an interthread socket (all platforms, even with fileevents !)
"

> I don't know whether there's an equivalent mechanism on
> Windows or not.

Unfortunately, not: in fact, it is the other way around: the various
fileevent pipes *need* to use reader threads (even in 8.1)!
I'm always baffled that it's so little known how crippled a platform
Win32 is...

-Alex

David Gravereaux

unread,
Mar 13, 1999, 3:00:00 AM3/13/99
to
Alexandre Ferrieux <alexandre...@cnet.francetelecom.fr> wrote:

> - David's AsyncMark/QueueEvent/PostMessage(WM_TIMER) (Win ONLY)


I should have mentioned the notifier bug on windows in the document.
The PostMessage() is only needed on the windows platform.

I should make a little example extension to show it, rather than just
explain it. The trouble is, I do all my coding in C++ with a bunch of
home-made and borrowed libraries. My coding style is very bizzare and
Win32 specific.

I'm always amazed how many ways there are to do the same thing in
programming. Thanks for the new approach, William.

An all platform, simple example extension in C would be a great
offering to "the community". Anyone interested?

* David Gravereaux *
Tomahawk Software Group

Mahalo Aloha from Maui, Hawai'i

Alexandre Ferrieux

unread,
Mar 15, 1999, 3:00:00 AM3/15/99
to
David Gravereaux wrote:
>
> An all platform, simple example extension in C would be a great
> offering to "the community". Anyone interested?

To be fair, any candidate should be warned in advance that "all
platform" actually means 'heavily #ifdef-ed'...<grin>

-Alex

Reply all
Reply to author
Forward
0 new messages