[John Buckman of Lyris Technology writes:]
Here is our scenario: we link Tcl into our multithreaded application, and
one thread launches Tcl and runs TclHttpd. This works great, but when our
application is told to exit, how do we tell TclHttpd to exit in a thread
safe manner? After all, we have TclHttpd in a "vwait forever" loop, from
which it never wants to exit
The solution that we adopted was to create a async event handler beforehand
to signal program exit, use a separate thread to notify the async event
when it is time to shutdown, and then Tcl runs our C code at a safe time
from inside its own thread, which modifies the global "forever" variable,
thus causing TclHttpd to gracefully exit. This works nicely, with no
memory leaks, and no need to wait for someone to cause an event.
We looked at the Tcl source code for the asynchronous operations, and all
of it is semaphore protected and thread safe (beautiful, clean source code,
by the way) so I assume that this is the proper way to do it since it
appears from looking at the Tcl code that it was made for this sort of thing.
[Colin McCormak writes:]
I've got a postgresql tcl interface which doesn't block on the db server
backend.
Postgresql uses a socket to communicate
with the backend.
I use Tcl_MakeTcpClientChannel to let Tcl supervise it, set
the appropriate options, and expose the postgresql C library
functions needed to tcl (using Swig.)
That way you can just treat it as a normal socket, as long as you let
Postgresql's library do the reading.
I've put up a copy here:
ftp://coldstore.sourceforge.net/pub/coldstore/libtclpq-20010120.tgz
I've requested a sourceforge project
libtclpq
I'll move it into cvs etc when the project's approved.
Some random points:
- it works with tcl8.4
- it works with the latest postgresql
- there's a tcl support suite (and a test file :)
- we're using it pretty heavily at $work
- it integrates with tclhttpd (although I've never integrated the
asynchronous behavior)
- there's also a set of itcl stuff (I'll release later) which gives the
ability to create widgets from tables, creates browsers, etc.