An important contstraint of the Tcl threads implementation is that only the thread that created a Tcl interpreter can use that interpreter. In other words, multiple threads can not access the same Tcl interpreter. (However, as was the case in previous releases, a single thread can safely create and use multiple interpreters.)
Tcl provides no special API for creating threads. When writing multithreaded applications incorporating Tcl, use the standard POSIX threads APIs on Unix systems and the standard Win32 threads APIs on Windows systems.
Tcl does provide Tcl_ExitThread and Tcl_FinalizeThread for terminating threads and invoking optional per-thread exit handlers. See the Tcl_Exit page for more information on these procedures.
Tcl provides Tcl_ThreadQueueEvent and Tcl_ThreadAlert for handling event queueing in multithreaded applications. See the Notifier manual page for more information on these procedures.
In this release, the Tcl language itself provides no support for creating multithreaded scripts (for example, scripts that could spawn a Tcl interpreter in a separate thread). If you need to add this feature at this time, see the tclThreadTest.c file in the Tcl source distribution for an experimental implementation of a Tcl "Thread" package implementing thread creation and management commands at the script level.
A condition variable is used as a signaling mechanism: a thread can lock a mutex and then wait on a condition variable with Tcl_ConditionWait. This atomically releases the mutex lock and blocks the waiting thread until another thread calls Tcl_ConditionNotify. The caller of Tcl_ConditionNotify should have the associated mutex held by previously calling Tcl_MutexLock, but this is not enforced. Notifying the condition variable unblocks all threads waiting on the condition variable, but they do not proceed until the mutex is released with Tcl_MutexUnlock. The implementation of Tcl_ConditionWait automatically locks the mutex before returning.
The caller of Tcl_ConditionWait should be prepared for spurious notifications by calling Tcl_ConditionWait within a while loop that tests some invariant.
The Tcl_GetThreadData call returns a pointer to a block of thread-private data. Its argument is a key that is shared by all threads and a size for the block of storage. The storage is automatically allocated and initialized to all zeros the first time each thread asks for it. The storage is automatically deallocated by Tcl_FinalizeThread.
Copyright © 1999 Scriptics Corporation Copyright © 1998 Sun Microsystems, Inc. Copyright © 1995-1997 Roger E. Critchlow Jr.