Thread Safety

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 52.  Tcl/Tk 8.1


The Tcl C library is thread-safe. This means you can use Tcl in an application that uses threads. The threading model for Tcl is that a thread can have one or more Tcl interpreters, but a Tcl interpreter cannot be used by different threads. For communication between threads, Tcl provides the ability to send Tcl scripts to an interpreter in another thread. You can also create an extension that implements shared data and does its own locking.

The Tcl C library provides mutex variables, condition variables, and thread local storage. These primatives are used by Tcl internally, and they are meant to be used by Tcl extensions to serialize access to their own data structures. The Tcl library allows different implementations of the threading primatives. This is done to support Unix, Windows, and Macintosh. Tcl uses native threads on Windows, and Posix pthreads on Unix. MacOS does not have true threads, so it is easy to provide the required thread API.

The testthread Command

Tcl 8.2 does not export threads to the script level, except through the testthread testing command. Instead of adding this to the Tcl core, this command will become an extension that is distributed along with Tcl. You can try out testthread by compiling the tcltest program instead of the regular tclsh shell. Table 52-1 describes the testthread operations, which are implemented in the generic/tclThreadTest.c file. These operations are likely to be similiar to the API provided by the more general threading extension, but you should check the documentation associated with that extension for more details.

Table 52-1. The testthread command.
testthread create ?script?Creates a new thread and a Tcl interpreter. Runs script after creating the Tcl interpreter. If no script is specified, the new thread waits with testthread wait.
testthread idReturns the thread ID of the current thread.
testthread errorproc procRegister proc as a hander for errors from other threads. If they terminate with a Tcl error, this procedure is called with the error message and errorInfo values as arguments. Otherwise a message is printed to stderr.
testthread exitTerminate the current thread.
testthread namesReturns a list of thread IDs.
testthread send id ?-async? scriptSend a script to another thread for evaluation. If -async is specified, the command does not wait for the result.
testthread waitEnter the event loop. This is used by worker threads to wait for scripts to arrive for evaluation.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net