1Threads(3)                  Tcl Library Procedures                  Threads(3)
2
3
4
5______________________________________________________________________________
6

NAME

8       Tcl_ConditionNotify, Tcl_ConditionWait, Tcl_ConditionFinalize, Tcl_Get‐
9       ThreadData, Tcl_MutexLock, Tcl_MutexUnlock, Tcl_MutexFinalize, Tcl_Cre‐
10       ateThread, Tcl_JoinThread - Tcl thread support.
11

SYNOPSIS

13       #include <tcl.h>
14
15       void
16       Tcl_ConditionNotify(condPtr)
17
18       void
19       Tcl_ConditionWait(condPtr, mutexPtr, timePtr)
20
21       void
22       Tcl_ConditionFinalize(condPtr)
23
24       Void *
25       Tcl_GetThreadData(keyPtr, size)
26
27       void
28       Tcl_MutexLock(mutexPtr)
29
30       void
31       Tcl_MutexUnlock(mutexPtr)
32
33       void
34       Tcl_MutexFinalize(mutexPtr)
35
36       int
37       Tcl_CreateThread(idPtr, threadProc, clientData, stackSize, flags)
38
39       int
40       Tcl_JoinThread(id, result)
41

ARGUMENTS

43       Tcl_Condition       *condPtr  (in)      A   condition  variable,  which
44                                               must be associated with a mutex
45                                               lock.
46
47       Tcl_Mutex           *mutexPtr (in)      A mutex lock.
48
49       Tcl_Time            *timePtr  (in)      A  time  limit on the condition
50                                               wait.  NULL  to  wait  forever.
51                                               Note  that a polling value of 0
52                                               seconds   doesn't   make   much
53                                               sense.
54
55       Tcl_ThreadDataKey   *keyPtr   (in)      This   identifies  a  block  of
56                                               thread local storage.  The  key
57                                               should  be  static and process-
58                                               wide, yet each thread will  end
59                                               up   associating   a  different
60                                               block of storage with this key.
61
62       int                 *size     (in)      The size of  the  thread  local
63                                               storage  block.  This amount of
64                                               data is allocated and  initial‐
65                                               ized  to  zero  the  first time
66                                               each  thread   calls   Tcl_Get‐
67                                               ThreadData.
68
69       Tcl_ThreadId        *idPtr    (out)     The  referred storage will con‐
70                                               tain the id of the  newly  cre‐
71                                               ated  thread as returned by the
72                                               operating system.
73
74       Tcl_ThreadId        id        (in)      Id of the thread waited upon.
75
76       Tcl_ThreadCreateProc          threadProc(in)
77                                               This procedure will act as  the
78                                               main()  of  the  newly  created
79                                               thread. The  specified  client‐
80                                               Data will be its sole argument.
81
82       ClientData          clientData(in)      Arbitrary  information.  Passed
83                                               as sole argument to the thread‐
84                                               Proc.
85
86       int                 stackSize (in)      The  size of the stack given to
87                                               the new thread.
88
89       int                 flags     (in)      Bitmask containing flags allow‐
90                                               ing the caller to modify behav‐
91                                               iour of the new thread.
92
93       int                 *result   (out)     The referred storage is used to
94                                               place  the  exit  code  of  the
95                                               thread waited upon into it.
96_________________________________________________________________
97

INTRODUCTION

99       Beginning with the 8.1 release, the Tcl  core  is  thread  safe,  which
100       allows  you  to incorporate Tcl into multithreaded applications without
101       customizing the Tcl core.  To enable Tcl  multithreading  support,  you
102       must  include the --enable-threads option to configure when you config‐
103       ure and compile your Tcl core.
104
105       An important constraint of the Tcl threads implementation is that  only
106       the thread that created a Tcl interpreter can use that interpreter.  In
107       other words, multiple threads can not access the same Tcl  interpreter.
108       (However,  as  was  the  case in previous releases, a single thread can
109       safely create and use multiple interpreters.)
110
111       Tcl does provide Tcl_CreateThread for creating threads. The caller  can │
112       determine  the size of the stack given to the new thread and modify the │
113       behaviour     through     the     supplied     flags.     The     value │
114       TCL_THREAD_STACK_DEFAULT  for  the stackSize indicates that the default │
115       size as specified by the operating system is to be  used  for  the  new │
116       thread.   As   for   the   flags,   currently   are   only  the  values │
117       TCL_THREAD_NOFLAGS and TCL_THREAD_JOINABLE defined. The first  of  them │
118       invokes  the  default  behaviour  with no specialties. Using the second │
119       value marks the new thread as joinable. This means that another  thread │
120       can wait for the such marked thread to exit and join it.                │
121
122       Restrictions: On some unix systems the pthread-library does not contain │
123       the functionality to specify the stacksize of a thread.  The  specified │
124       value  for  the stacksize is ignored on these systems. Both Windows and │
125       Macintosh currently do not support joinable threads. This flag value is │
126       therefore ignored on these platforms.
127
128       Tcl  does provide Tcl_ExitThread and Tcl_FinalizeThread for terminating
129       threads and  invoking  optional  per-thread  exit  handlers.   See  the
130       Tcl_Exit page for more information on these procedures.
131
132       The  Tcl_JoinThread  function is provided to allow threads to wait upon │
133       the exit of another thread, which must have  been  marked  as  joinable │
134       through  usage  of the TCL_THREAD_JOINABLE-flag during its creation via │
135       Tcl_CreateThread.                                                       │
136
137       Trying to wait for the exit of a non-joinable thread or a thread  which │
138       is  already waited upon will result in an error. Waiting for a joinable │
139       thread which already exited is possible, the  system  will  retain  the │
140       necessary  information  until  after  the call to Tcl_JoinThread.  This │
141       means that not calling Tcl_JoinThread for a joinable thread will  cause │
142       a memory leak.
143
144       Tcl  provides  Tcl_ThreadQueueEvent  and  Tcl_ThreadAlert  for handling
145       event queueing in multithreaded applications.  See the Notifier  manual
146       page for more information on these procedures.
147
148       In this release, the Tcl language itself provides no support for creat‐
149       ing multithreaded scripts (for example, scripts that could spawn a  Tcl
150       interpreter  in a separate thread).  If you need to add this feature at
151       this time, see the tclThreadTest.c file in the Tcl source  distribution
152       for an experimental implementation or use the Tcl "Threading Extension"
153       package implementing thread creation and  management  commands  at  the
154       script level.
155
156
157

DESCRIPTION

159       A mutex is a lock that is used to serialize all threads through a piece
160       of code by calling Tcl_MutexLock and Tcl_MutexUnlock.   If  one  thread
161       holds  a mutex, any other thread calling Tcl_MutexLock will block until
162       Tcl_MutexUnlock is called.  A mutex can be destroyed after its  use  by │
163       calling  Tcl_MutexFinalize.   The  result of locking a mutex twice from │
164       the same thread is undefined.  On some platforms it will  result  in  a │
165       deadlock.   The  Tcl_MutexLock,  Tcl_MutexUnlock  and Tcl_MutexFinalize
166       procedures are defined as empty macros if not  compiling  with  threads
167       enabled.  For declaration of mutexes the TCL_DECLARE_MUTEX macro should
168       be used.  This macro assures correct mutex handling even when the  core
169       is compiled without threads enabled.
170
171       A  condition  variable  is  used as a signaling mechanism: a thread can
172       lock a mutex and then wait on a condition variable with  Tcl_Condition‐
173       Wait.   This  atomically releases the mutex lock and blocks the waiting
174       thread until another thread calls Tcl_ConditionNotify.  The  caller  of
175       Tcl_ConditionNotify should have the associated mutex held by previously
176       calling Tcl_MutexLock, but this is not enforced.  Notifying the  condi‐
177       tion  variable  unblocks all threads waiting on the condition variable,
178       but they do not proceed until the mutex is released  with  Tcl_MutexUn‐
179       lock.   The implementation of Tcl_ConditionWait automatically locks the
180       mutex before returning.
181
182       The caller of Tcl_ConditionWait should be prepared for spurious notifi‐
183       cations  by  calling  Tcl_ConditionWait  within a while loop that tests
184       some invariant.
185
186       A condition variable can be destroyed after its use by calling Tcl_Con‐ 
187       ditionFinalize.                                                         │
188
189       The  Tcl_ConditionNotify,  Tcl_ConditionWait  and Tcl_ConditionFinalize 
190       procedures are defined as empty macros if not  compiling  with  threads │
191       enabled.
192
193       The  Tcl_GetThreadData call returns a pointer to a block of thread-pri‐
194       vate data.  Its argument is a key that is shared by all threads  and  a
195       size  for the block of storage.  The storage is automatically allocated
196       and initialized to all zeros the first time each thread  asks  for  it.
197       The storage is automatically deallocated by Tcl_FinalizeThread.
198

INITIALIZATION

200       All  of  these synchronization objects are self initializing.  They are
201       implemented as opaque pointers that should be NULL upon first use.  The
202       mutexes  and  condition variables are either cleaned up by process exit │
203       handlers (if living that long) or explicitly by calls  to  Tcl_MutexFi‐ 
204       nalize  or  Tcl_ConditionFinalize.   Thread  local storage is reclaimed
205       during Tcl_FinalizeThread.
206

CREATING THREADS

208       The API to create threads is not finalized at  this  time.   There  are
209       private  facilities  to  create  threads  that contain a new Tcl inter‐
210       preter, and to send scripts among threads.  Dive  into  tclThreadTest.c
211       and tclThread.c for examples.
212

SEE ALSO

214       Tcl_GetCurrentThread,  Tcl_ThreadQueueEvent, Tcl_ThreadAlert, Tcl_Exit‐
215       Thread,        Tcl_FinalizeThread,         Tcl_CreateThreadExitHandler,
216       Tcl_DeleteThreadExitHandler
217

KEYWORDS

219       thread, mutex, condition variable, thread local storage
220
221
222
223Tcl                                   8.1                           Threads(3)
Impressum