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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
<HTML><HEAD>
<TITLE>Tcl API Reference -- ns_thread</TITLE>
<LINK rel=Previous href="tapi-138.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="tapi-140.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="tapi-138.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tapi-140.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="82563">
</a><h3>ns_thread</h3>
<a name="82564">
</a><h4>Overview</h4>
<p><a name="82565">
</a>Manage threads</p>
<a name="82566">
</a><h4>Syntax</h4>
<p><a name="82567">
</a><b>ns_thread begin</b> <i>script</i></p>
<p><a name="82568">
</a><b>ns_thread begindetached</b> <i>script</i></p>
<p><a name="82569">
</a><b>ns_thread get</b></p>
<p><a name="82570">
</a><b>ns_thread getid</b></p>
<p><a name="82571">
</a><b>ns_thread wait</b> <i>tid</i></p>
<p><a name="133149">
</a><b>ns_thread yield</b></p>
<a name="82572">
</a><h4>Description</h4>
<p><a name="82573">
</a><b>ns_thread begin</b> begins a new thread which evaluates the specified script and then exits. It returns a thread ID that must eventually be passed to <b>ns_thread wait</b>. (Failing to call <b>ns_thread wait</b> will eventually result in no new threads being created.)</p>
<p><a name="82574">
</a><b>ns_thread begindetached</b> begins a detached thread that doesn't have to be (and can't be) waited for.</p>
<p><a name="82575">
</a><b>ns_thread get</b> gets the thread ID of the current thread. The result is a thread ID that can be passed to <b>ns_thread wait</b> and may look something like "tid532".</p>
<p><a name="82576">
</a><b>ns_thread getid</b> gets the thread integer number for the current thread. The result is a small integer used for identifying threads is a human-readable way, such as "1" or "1120", for example.</p>
<p><a name="82577">
</a><b>ns_thread wait</b> waits for the specified thread to exit. The <i>tid</i> argument is a thread ID returned by <b>ns_thread begin</b> or <b>ns_thread get</b>.</p>
<p><a name="133150">
</a><b>ns_thread yield</b> causes the current thread to yield.</p>
<a name="82579">
</a><h4>Example</h4>
<p><a name="82582">
</a>This example is similar to the example under the <b>ns_sockselect</b> function (see <a href="tapi-136.htm#82277">page 205</a>) of connecting to the 10 servers and waiting to service them with the <b>ns_sockselect</b> command. In this case, though, each connection gets it's own thread.</p>
<pre> <a name="82584"></a># This is the procedure which is evaluated for each thread and
<a name="82585"></a># handles a single connection to host number $i
<a name="82586"></a>
<a name="82587"></a>proc getpage {i} {
<a name="82588"></a> global pages
<a name="82589"></a> # new thread will start here - first connect to host
<a name="82590"></a> set host [format "www%2d.foo.com" $i]
<a name="82591"></a> set fds [ns_sockopen $host 80
<a name="82592"></a> set r [lindex $fds 0]
<a name="82593"></a> set w [lindex $fds 1]
<a name="82594"></a> # next, send request
<a name="82595"></a> puts $w "GET /index.htm HTTP/1.0\r\n\r"
<a name="82596"></a> flush $w
<a name="82597"></a> # then read page
<a name="82598"></a> set pages($i) [read $r]
<a name="82599"></a> # and close sockets
<a name="82600"></a> close $w
<a name="82601"></a> close $r
<a name="82602"></a> # thread goes away here and other threads waiting
<a name="82603"></a> # on ns_thread wait will wakeup
<a name="82604"></a>}
<a name="82605"></a>
<a name="82606"></a># Here's the loop which creates the threads which run getpage.
<a name="82607"></a>for {set i 1} {$i < 9} {incr i} {
<a name="82608"></a> set tids($i) [ns_thread begin "getpage $i"]
<a name="82609"></a>}
<a name="82610"></a>
<a name="82611"></a># wait for the threads to exit and then process the pages
<a name="82612"></a>for {set i 1} {$i < 9} {incr i} {
<a name="82613"></a> ns_thread wait $tids($i)
<a name="82614"></a> # output page
<a name="82615"></a> ... process the page in $pages($i) put there by other
<a name="82616"></a> thread ...
<a name="82617"></a>}
</pre><p><p><a name="82618">
</a>Note that the code here is much simplier to follow than the <b>ns_sockselect</b> example; that's the benefit of multithreaded programming. However, it uses more resources as threads need to be created and initialized. This is generally not a problem unless you plan to create many threads.</p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="tapi-138.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tapi-140.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|