📄 thread.3
字号:
'\"'\" Copyright (c) 1999 Scriptics Corporation'\" Copyright (c) 1998 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" RCS: @(#) $Id: Thread.3,v 1.14 2002/07/01 18:24:39 jenglish Exp $'\" .so man.macros.TH Threads 3 "8.1" Tcl "Tcl Library Procedures".BS.SH NAMETcl_ConditionNotify, Tcl_ConditionWait, Tcl_ConditionFinalize, Tcl_GetThreadData, Tcl_MutexLock, Tcl_MutexUnlock, Tcl_MutexFinalize, Tcl_CreateThread, Tcl_JoinThread \- Tcl thread support..SH SYNOPSIS.nf\fB#include <tcl.h>\fR.spvoid\fBTcl_ConditionNotify\fR(\fIcondPtr\fR).spvoid\fBTcl_ConditionWait\fR(\fIcondPtr, mutexPtr, timePtr\fR).spvoid\fBTcl_ConditionFinalize\fR(\fIcondPtr\fR).spVoid *\fBTcl_GetThreadData\fR(\fIkeyPtr, size\fR).spvoid\fBTcl_MutexLock\fR(\fImutexPtr\fR).spvoid\fBTcl_MutexUnlock\fR(\fImutexPtr\fR).spvoid\fBTcl_MutexFinalize\fR(\fImutexPtr\fR).spint\fBTcl_CreateThread\fR(\fIidPtr, threadProc, clientData, stackSize, flags\fR).spint\fBTcl_JoinThread\fR(\fIid, result\fR).SH ARGUMENTS.AS Tcl_ThreadDataKey *keyPtr.AP Tcl_Condition *condPtr inA condition variable, which must be associated with a mutex lock..AP Tcl_Condition *mutexPtr inA mutex lock..AP Tcl_Time *timePtr inA time limit on the condition wait. NULL to wait forever.Note that a polling value of 0 seconds doesn't make much sense..AP Tcl_ThreadDataKey *keyPtr inThis identifies a block of thread local storage. The key should bestatic and process-wide, yet each thread will end up associatinga different block of storage with this key..AP int *size inThe size of the thread local storage block. This amount of datais allocated and initialized to zero the first time each threadcalls \fBTcl_GetThreadData\fR..AP Tcl_ThreadId *idPtr outThe referred storage will contain the id of the newly created thread asreturned by the operating system..AP Tcl_ThreadId id inId of the thread waited upon..AP Tcl_ThreadCreateProc threadProc inThis procedure will act as the \fBmain()\fR of the newly createdthread. The specified \fIclientData\fR will be its sole argument..AP ClientData clientData inArbitrary information. Passed as sole argument to the \fIthreadProc\fR..AP int stackSize inThe size of the stack given to the new thread..AP int flags inBitmask containing flags allowing the caller to modify behaviour ofthe new thread..AP int *result outThe referred storage is used to place the exit code of the threadwaited upon into it..BE.SH INTRODUCTIONBeginning with the 8.1 release, the Tcl core is thread safe, whichallows you to incorporate Tcl into multithreaded applications withoutcustomizing the Tcl core. To enable Tcl multithreading support,you must include the \fB--enable-threads\fR option to \fBconfigure\fRwhen you configure and compile your Tcl core..PPAn important constraint of the Tcl threads implementation is that\fIonly the thread that created a Tcl interpreter can use thatinterpreter\fR. In other words, multiple threads can not accessthe same Tcl interpreter. (However, as was the case in previousreleases, a single thread can safely create and use multipleinterpreters.).PP.VS 8.3.1Tcl does provide \fBTcl_CreateThread\fR for creating threads. Thecaller can determine the size of the stack given to the new thread andmodify the behaviour through the supplied \fIflags\fR. The value\fBTCL_THREAD_STACK_DEFAULT\fR for the \fIstackSize\fR indicates thatthe default size as specified by the operating system is to be usedfor the new thread. As for the flags, currently are only the values\fBTCL_THREAD_NOFLAGS\fR and \fBTCL_THREAD_JOINABLE\fR defined. Thefirst of them invokes the default behaviour with nospecialties. Using the second value marks the new thread as\fIjoinable\fR. This means that another thread can wait for the suchmarked thread to exit and join it..PPRestrictions: On some unix systems the pthread-library does notcontain the functionality to specify the stacksize of a thread. Thespecified value for the stacksize is ignored on these systems. BothWindows and Macintosh currently do not support joinable threads. Thisflag value is therefore ignored on these platforms..VE.PPTcl does provide \fBTcl_ExitThread\fR and \fBTcl_FinalizeThread\fRfor terminating threads and invoking optional per-thread exithandlers. See the \fBTcl_Exit\fR page for more information on theseprocedures..PP.VSThe \fBTcl_JoinThread\fR function is provided to allow threads to waitupon the exit of another thread, which must have been marked asjoinable through usage of the \fBTCL_THREAD_JOINABLE\fR-flag duringits creation via \fBTcl_CreateThread\fR..PPTrying to wait for the exit of a non-joinable thread or a thread whichis already waited upon will result in an error. Waiting for a joinablethread which already exited is possible, the system will retain thenecessary information until after the call to \fBTcl_JoinThread\fR.This means that not calling \fBTcl_JoinThread\fR for a joinable threadwill cause a memory leak..VE.PPTcl provides \fBTcl_ThreadQueueEvent\fR and \fBTcl_ThreadAlert\fRfor handling event queueing in multithreaded applications. Seethe \fBNotifier\fR manual page for more information on these procedures..PPIn this release, the Tcl language itself provides no support forcreating multithreaded scripts (for example, scripts that could spawna Tcl interpreter in a separate thread). If you need to add thisfeature at this time, see the \fItclThreadTest.c\fRfile in the Tcl source distribution for an experimental implementationof a Tcl "Thread" package implementing thread creation and managementcommands at the script level..SH DESCRIPTIONA mutex is a lock that is used to serialize all threads through a pieceof code by calling \fBTcl_MutexLock\fR and \fBTcl_MutexUnlock\fR.If one thread holds a mutex, any other thread calling \fBTcl_MutexLock\fR willblock until \fBTcl_MutexUnlock\fR is called..VSA mutex can be destroyed after its use by calling \fBTcl_MutexFinalize\fR.The result of locking a mutex twice from the same thread is undefined.On some platforms it will result in a deadlock..VEThe \fBTcl_MutexLock\fR, \fBTcl_MutexUnlock\fR and \fBTcl_MutexFinalize\fRprocedures are defined as empty macros if not compiling with threads enabled..PPA condition variable is used as a signaling mechanism:a thread can lock a mutex and then wait on a condition variablewith \fBTcl_ConditionWait\fR. This atomically releases the mutex lockand blocks the waiting thread until another thread calls\fBTcl_ConditionNotify\fR. The caller of \fBTcl_ConditionNotify\fR shouldhave the associated mutex held by previously calling \fBTcl_MutexLock\fR,but this is not enforced. Notifying thecondition variable unblocks all threads waiting on the condition variable,but they do not proceed until the mutex is released with \fBTcl_MutexUnlock\fR.The implementation of \fBTcl_ConditionWait\fR automatically locksthe mutex before returning..PPThe caller of \fBTcl_ConditionWait\fR should be prepared for spuriousnotifications by calling \fBTcl_ConditionWait\fR within a while loopthat tests some invariant..PP.VSA condition variable can be destroyed after its use by calling\fBTcl_ConditionFinalize\fR..PPThe \fBTcl_ConditionNotify\fR, \fBTcl_ConditionWait\fR and\fBTcl_ConditionFinalize\fR procedures are defined as empty macros ifnot compiling with threads enabled..VE.PPThe \fBTcl_GetThreadData\fR call returns a pointer to a block ofthread-private data. Its argument is a key that is shared by all threadsand 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 \fBTcl_FinalizeThread\fR..SH INITIALIZATION.PPAll of these synchronization objects are self initializing.They are implemented as opaque pointers that should be NULLupon first use.The mutexes and condition variables are.VSeither cleaned up by process exit handlers (if living that long) orexplicitly by calls to \fBTcl_MutexFinalize\fR or\fBTcl_ConditionFinalize\fR..VEThread local storage is reclaimed during \fBTcl_FinalizeThread\fR..SH "CREATING THREADS"The API to create threads is not finalized at this time.There are private facilities to create threads that contain a newTcl interpreter, and to send scripts among threads.Dive into tclThreadTest.c and tclThread.c for examples..SH "SEE ALSO"Tcl_GetCurrentThread, Tcl_ThreadQueueEvent, Tcl_ThreadAlert,Tcl_ExitThread, Tcl_FinalizeThread,Tcl_CreateThreadExitHandler, Tcl_DeleteThreadExitHandler.SH KEYWORDSthread, mutex, condition variable, thread local storage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -