Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

ns_thread(3aolserv) [debian man page]

ns_thread(3aolserver)					    AOLserver Built-In Commands 				     ns_thread(3aolserver)

__________________________________________________________________________________________________________________________________________________

NAME
ns_thread - commands SYNOPSIS
ns_thread begin script ns_thread begindetached script ns_thread get ns_thread getid ns_thread wait tid ns_thread yield _________________________________________________________________ DESCRIPTION
ns_thread begin: begins a new thread which evaluates the specified script and then exits. It returns a thread ID that must eventually be passed to ns_thread wait. (Failing to call ns_thread wait will eventually result in no new threads being created.) ns_thread begindetached: begins a detached thread that doesn't have to be (and can't be) waited for. ns_thread get: gets the thread ID of the current thread. The result is a thread ID that can be passed to ns_thread wait and may look something like "tid532". ns_thread getid: gets the thread integer number for the current thread. The result is a small integer used for identifying threads is a human-read- able way, such as "1" or "1120", for example. ns_thread wait: waits for the specified thread to exit. The tid argument is a thread ID returned by ns_thread begin or ns_thread get. ns_thread yield: causes the current thread to yield. EXAMPLES
This example is similar to the example under the ns_sockselect function of connecting to the 10 servers and waiting to service them with the ns_sockselect command. In this case, though, each connection gets it's own thread. # This is the procedure which is evaluated for each thread and # handles a single connection to host number $i proc getpage {i} { global pages # new thread will start here - first connect to host set host [format "www%2d.foo.com" $i] set fds [ns_sockopen $host 80 set r [lindex $fds 0] set w [lindex $fds 1] # next, send request 0r" puts $w "GET /index.htm HTTP/1.0 flush $w # then read page set pages($i) [read $r] # and close sockets close $w close $r # thread goes away here and other threads waiting # on ns_thread wait will wakeup } # Here's the loop which creates the threads which run getpage. for {set i 1} {$i < 9} {incr i} { set tids($i) [ns_thread begin "getpage $i"] } # wait for the threads to exit and then process the pages for {set i 1} {$i < 9} {incr i} { ns_thread wait $tids($i) # output page ... process the page in $pages($i) put there by other thread ... } Note that the code here is much simpler to follow than the ns_sockselect example; that's the benefit of multithreaded programming. However, it uses more resources as threads need to be created and initialized. This can be a problem if you plan to create many threads. SEE ALSO
KEYWORDS
threads AOLserver 4.0 ns_thread(3aolserver)

Check Out this Related Man Page

thr_join(3C)						   Standard C Library Functions 					      thr_join(3C)

NAME
thr_join - wait for thread termination SYNOPSIS
cc -mt [ flag... ] file...[ library... ] #include <thread.h> int thr_join(thread_t thread, thread_t *departed, void **status); DESCRIPTION
The thr_join() function suspends processing of the calling thread until the target thread completes. The thread argument must be a member of the current process and cannot be a detached thread. See thr_create(3C). If two or more threads wait for the same thread to complete, all will suspend processing until the thread has terminated, and then one thread will return successfully and the others will return with an error of ESRCH. The thr_join() function will not block processing of the calling thread if the target thread has already terminated. If a thr_join() call returns successfully with a non-null status argument, the value passed to thr_exit(3C) by the terminating thread will be placed in the location referenced by status. If the target thread ID is 0, thr_join() finds and returns the status of a terminated undetached thread in the process. If no such thread exists, it suspends processing of the calling thread until a thread for which no other thread is waiting enters that state, at which time it returns successfully, or until all other threads in the process are either daemon threads or threads waiting in thr_join(), in which case it returns EDEADLK. See NOTES. If departed is not NULL, it points to a location that is set to the ID of the terminated thread if thr_join() returns successfully. RETURN VALUES
If successful, thr_join() returns 0. Otherwise, an error number is returned to indicate the error. ERRORS
EDEADLK A joining deadlock would occur, such as when a thread attempts to wait for itself, or the calling thread is waiting for any thread to exit and only daemon threads or waiting threads exist in the process. ESRCH No undetached thread could be found corresponding to the given thread ID. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
thr_create(3C), thr_exit(3C), wait(3C), attributes(5), standards(5) NOTES
Using thr_join(3C) in the following syntax, while (thr_join(0, NULL, NULL) == 0); will wait for the termination of all non-daemon threads, excluding threads that are themselves waiting in thr_join(). SunOS 5.10 27 Mar 2000 thr_join(3C)
Man Page