Manual Pages for UNIX Darwin command on man Tcl_ConditionNotify
MyWebUniversity

Manual Pages for UNIX Darwin command on man Tcl_ConditionNotify

Threads(3) Tcl Library Procedures Threads(3)

NAME

TclConditionNotify, TclConditionWait, TclConditionFinalize, TclGet-

ThreadData, TclMutexLock, TclMutexUnlock, TclMutexFinalize, TclCre-

ateThread, TclJoinThread - Tcl thread support.

SYNOPSIS

##iinncclluuddee <>

void TTccllCCoonnddiittiioonnNNoottiiffyy(condPtr) void TTccllCCoonnddiittiioonnWWaaiitt(condPtr, mutexPtr, timePtr) void TTccllCCoonnddiittiioonnFFiinnaalliizzee(condPtr) Void * TTccllGGeettTThhrreeaaddDDaattaa(keyPtr, size) void TTccllMMuutteexxLLoocckk(mutexPtr) void TTccllMMuutteexxUUnnlloocckk(mutexPtr) void TTccllMMuutteexxFFiinnaalliizzee(mutexPtr) int TTccllCCrreeaatteeTThhrreeaadd(idPtr, threadProc, clientData, stackSize, flags) int TTccllJJooiinnTThhrreeaadd(id, result) AARRGGUUMMEENNTTSS TclCondition *condPtr (in) A condition variable, which must be associated with a mutex lock. TclCondition *mutexPtr (in) A mutex lock. TclTime *timePtr (in) A time limit on the condition wait. NULL to wait forever. Note that a polling value of 0 seconds doesn't make much sense. TclThreadDataKey *keyPtr (in) This identifies a block of thread local storage. The key

should be static and process-

wide, yet each thread will end up associating a different block of storage with this key. int *size (in) The size of the thread local storage block. This amount of

data is allocated and initial-

ized to zero the first time

each thread calls TTccllGGeett-

TThhrreeaaddDDaattaa.

TclThreadId *idPtr (out) The referred storage will con-

tain the id of the newly cre-

ated thread as returned by the operating system. TclThreadId id (in) Id of the thread waited upon. TclThreadCreateProc threadProc(in) This procedure will act as the mmaaiinn(()) of the newly created

thread. The specified client-

Data will be its sole argument. ClientData clientData(in) Arbitrary information. Passed

as sole argument to the thread-

Proc. int stackSize (in) The size of the stack given to the new thread.

int flags (in) Bitmask containing flags allow-

ing the caller to modify behav-

iour of the new thread. int *result (out) The referred storage is used to place the exit code of the thread waited upon into it. IINNTTRROODDUUCCTTIIOONN Beginning with the 8.1 release, the Tcl core is thread safe, which allows you to incorporate Tcl into multithreaded applications without customizing the Tcl core. To enable Tcl multithreading support, you

must include the --eennaabbllee-tthhrreeaaddss option to ccoonnffiigguurree when you config-

ure and compile your Tcl core. An important constraint 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 does provide TTccllCCrreeaatteeTThhrreeaadd for creating threads. The caller can | determine the size of the stack given to the new thread and modify the | behaviour through the supplied flags. The value | TTCCLLTTHHRREEAADDSSTTAACCKKDDEEFFAAUULLTT for the stackSize indicates that the default | size as specified by the operating system is to be used for the new | thread. As for the flags, currently are only the values | TTCCLLTTHHRREEAADDNNOOFFLLAAGGSS and TTCCLLTTHHRREEAADDJJOOIINNAABBLLEE defined. The first of them | invokes the default behaviour with no specialties. Using the second | value marks the new thread as joinable. This means that another thread | can wait for the such marked thread to exit and join it. |

Restrictions: On some unix systems the pthread-library does not contain |

the functionality to specify the stacksize of a thread. The specified | value for the stacksize is ignored on these systems. Both Windows and | Macintosh currently do not support joinable threads. This flag value is | therefore ignored on these platforms. Tcl does provide TTccllEExxiittTThhrreeaadd and TTccllFFiinnaalliizzeeTThhrreeaadd for terminating

threads and invoking optional per-thread exit handlers. See the

TTccllEExxiitt page for more information on these procedures. The TTccllJJooiinnTThhrreeaadd function is provided to allow threads to wait upon | the exit of another thread, which must have been marked as joinable |

through usage of the TTCCLLTTHHRREEAADDJJOOIINNAABBLLEE-flag during its creation via |

TTccllCCrreeaatteeTThhrreeaadd. |

Trying to wait for the exit of a non-joinable thread or a thread which |

is already waited upon will result in an error. Waiting for a joinable | thread which already exited is possible, the system will retain the | necessary information until after the call to TTccllJJooiinnTThhrreeaadd. This | means that not calling TTccllJJooiinnTThhrreeaadd for a joinable thread will cause | a memory leak. Tcl provides TTccllTThhrreeaaddQQuueeuueeEEvveenntt and TTccllTThhrreeaaddAAlleerrtt for handling event queueing in multithreaded applications. See the NNoottiiffiieerr manual page for more information on these procedures.

In this release, the Tcl language itself provides no support for creat-

ing 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 implement-

ing thread creation and management commands at the script level.

DESCRIPTION

A mutex is a lock that is used to serialize all threads through a piece of code by calling TTccllMMuutteexxLLoocckk and TTccllMMuutteexxUUnnlloocckk. If one thread holds a mutex, any other thread calling TTccllMMuutteexxLLoocckk will block until TTccllMMuutteexxUUnnlloocckk is called. A mutex can be destroyed after its use by | calling TTccllMMuutteexxFFiinnaalliizzee. The result of locking a mutex twice from | the same thread is undefined. On some platforms it will result in a | deadlock. The TTccllMMuutteexxLLoocckk, TTccllMMuutteexxUUnnlloocckk and TTccllMMuutteexxFFiinnaalliizzee procedures are defined as empty macros if not compiling with threads enabled. A condition variable is used as a signaling mechanism: a thread can

lock a mutex and then wait on a condition variable with TTccllCCoonnddiittiioonn-

WWaaiitt. This atomically releases the mutex lock and blocks the waiting thread until another thread calls TTccllCCoonnddiittiioonnNNoottiiffyy. The caller of TTccllCCoonnddiittiioonnNNoottiiffyy should have the associated mutex held by previously

calling TTccllMMuutteexxLLoocckk, but this is not enforced. Notifying the condi-

tion variable unblocks all threads waiting on the condition variable,

but they do not proceed until the mutex is released with TTccllMMuutteexxUUnn-

lloocckk. The implementation of TTccllCCoonnddiittiioonnWWaaiitt automatically locks the mutex before returning.

The caller of TTccllCCoonnddiittiioonnWWaaiitt should be prepared for spurious notifi-

cations by calling TTccllCCoonnddiittiioonnWWaaiitt within a while loop that tests some invariant.

A condition variable can be destroyed after its use by calling TTccllCCoonn- |

ddiittiioonnFFiinnaalliizzee. | The TTccllCCoonnddiittiioonnNNoottiiffyy, TTccllCCoonnddiittiioonnWWaaiitt and TTccllCCoonnddiittiioonnFFiinnaalliizzee | procedures are defined as empty macros if not compiling with threads | enabled.

The TTccllGGeettTThhrreeaaddDDaattaa call returns a pointer to a block of thread-pri-

vate 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 TTccllFFiinnaalliizzeeTThhrreeaadd. IINNIITTIIAALLIIZZAATTIIOONN All of these synchronization objects are self initializing. They are implemented as opaque pointers that should be NULL upon first use. The mutexes and condition variables are either cleaned up by process exit |

handlers (if living that long) or explicitly by calls to TTccllMMuutteexxFFii- |

nnaalliizzee or TTccllCCoonnddiittiioonnFFiinnaalliizzee. Thread local storage is reclaimed during TTccllFFiinnaalliizzeeTThhrreeaadd. CCRREEAATTIINNGG TTHHRREEAADDSS The API to create threads is not finalized at this time. There are

private facilities to create threads that contain a new Tcl inter-

preter, and to send scripts among threads. Dive into tclThreadTest.c and tclThread.c for examples.

SEE ALSO

TclGetCurrentThread, TclThreadQueueEvent, TclThreadAlert, TclExit-

Thread, TclFinalizeThread, TclCreateThreadExitHandler, TclDeleteThreadExitHandler KKEEYYWWOORRDDSS thread, mutex, condition variable, thread local storage Tcl 8.1 Threads(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™