B.2.7 Example Usage

Figure [*] shows an example hello-world-like child thread. As noted earlier, each thread is allocated its own stack, so each thread has its own private arg argument and myarg variable. Each child simply prints its argument and its smp_thread_id() before exiting. Note that the return statement on line 7 terminates the thread, returning a NULL to whoever invokes wait_thread() on this thread.

Figure: Example Child Thread
\begin{figure}{ \scriptsize
\begin{verbatim}1 void *thread_test(void *arg)
2...
...,
6 myarg, smp_thread_id());
7 return NULL;
8 }\end{verbatim}
}\end{figure}

The parent program is shown in Figure [*]. It invokes smp_init() to initialize the threading system on line 6, parse arguments on lines 7-14, and announces its presence on line 15. It creates the specified number of child threads on lines 16-17, and waits for them to complete on line 18. Note that wait_all_threads() discards the threads return values, as in this case they are all NULL, which is not very interesting.

Figure: Example Parent Thread
\begin{figure}{ \scriptsize
\begin{verbatim}1 int main(int argc, char *argv[]...
...threads completed.\n'', nkids);
20 exit(0);
21 }\end{verbatim}
}\end{figure}



Paul E. McKenney 2011-12-16