|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
<HTML><HEAD><TITLE>Tcl Commands - thread manual page</TITLE></HEAD><BODY>
<H3><A NAME="M2">NAME</A></H3>
thread - Create and manipulate threads with Tcl interpreters in them.
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>thread create</B><I>?script?</I><BR>
<B>thread id</B><BR>
<B>thread errorproc</B> <I>procname</I><BR>
<B>thread exit</B><BR>
<B>thread names</B><BR>
<B>thread send</B> <I>id </I><B>?-async?</B><I> script</I><BR>
<B>thread wait</B><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The
<b>thread</b>
command creates threads that contain Tcl interpreters, and it lets you
send scripts to those threads.
<p>
<b>thread create</b> creates a thread that contains a Tcl interpreter.
The Tcl interpreter either evaluates the <i>script</i>, if specified,
or it waits in the event loop for scripts that arrive via the
<b>thread send</b> command.
The result of <b>thread create</b> is the ID of the thread.
The result, if any, of <i>script</i> is ignored.
<p>
<b>thread id</b> returns the ID of the current thread.
<p>
<b>thread errorproc</b> sets a handler for errors that occur in other
threads. By default, an uncaught error in a thread terminates that thread
and causes an error message to be sent to the standard error channel.
You can change the default reporting scheme by registering a procedure
that is called to report the error.
The <i>proc</i> is called in the interpreter that invoked the
<b>thread errorproc</b> command. The original thread that has the
uncaught error is terminated in any case.
The <i>proc</i> is called like this:
<ul>
<pre>
myerroproc thread_id errorInfo
</pre>
</ul>
<p>
<b>thread exit</b> terminates the current thread.
There is no way to force another thread to exit - you can only
ask it to terminate by sending it a command.
<p>
<b>thread names</b> returns a list of thread IDs.
These are only for threads that have been created via <b>thread create</b>.
If your application creates other threads at the C level, they
are not reported by <b>thread names</b>.
<p>
<b>thread send</b> passes a <i>script</i> to another thread and,
optionally, waits for the result. If the <b>-async</b> flag is specified
then the caller does not wait for the result. The target thread must
enter its event loop in order to receive script messages. This is done
by default for threads created without a startup script. Threads can
enter the event loop explicitly by calling
<b>thread wait</b> or <b>vwait</b>.
<p>
<b>thread wait</b> enters the event loop so a thread can receive
messages from <b>thread send</b>. This is equivalent to
<b>vwait</b> <i>unusedvariable</i>.
<H3><A NAME="M4">DISCUSSION</A></H3>
The fundamental threading model in Tcl
is that there can be one or more Tcl interpreters per thread, but
each Tcl interpreter should only be used by a single thread.
A "shared memory" abstraction is awkward to provide in Tcl because
Tcl makes assumptions about variable and data ownership.
Therefore this extension supports a simple form of threading where
the main thread can manage several background, or "worker" threads.
For example, an event-driven server can pass requests to worker threads,
and then await responses from worker threads or new client requests.
Everything goes through the common Tcl event loop, so message
passing between threads works naturally with event-driven I/O,
<b>vwait</b> on variables, and so forth.
<h3>SEE ALSO</h3>
<a href=http://www.scriptics.com/support/howto/thread_model.html>
A Guide to the Tcl Threading Model</a>.
</BODY></HTML>
|