Information Technology Reference
In-Depth Information
voidsthreadcreate(thread,func,arg)
Create a new thread, storing information about it in thread . The thread
will execute the function func , which will be called with the argument arg .
voidsthreadyield()
The calling thread voluntarily gives up the processor to let some other
thread(s) run. The scheduler can resume running the calling thread when-
ever it chooses to do so.
intsthreadjoin(thread)
Wait for the specified thread thread to finish if it has not already done so;
then return the value passed to sthreadexit() by the specified thread
thread .
Note that sthreadjoin() may be called only once for each
thread .
voidsthreadexit(ret)
Finish the current thread. Store the the value ret in the current thread's
data structure and, if another thread is already waiting in a call to
sthreadjoin() , then wake it up so that it can run and return this value.
Figure4.5: Simple API for using threads called sthreads (\simple threads.")
If you want to run these examples or write your own, the sthread library code
is available at sthread.h and sthread.c.
on Posix, most other threads packages are quite similar; if you understand how
to program with sthreads, you will find it easy to write code with most standard
APIs for threads.
4.2.1
A simple multi-threaded program
Figure 4.6 shows a simple multi-threaded program written in 'C' using sthreads.
The main() function uses sthreadcreate() to create 10 threads.
The
interesting arguments are the second and third.
The second argument, go , is a pointer to the function go() , which is the
function in which the newly-created thread should begin execution.
The third argument, ii , is the argument that should be passed to that
function.
Thus, sthreadcreate() will initialize the ii th thread's state so that it is
prepared to call the function go() with the argument ii .
 
Search WWH ::




Custom Search