%
% This file was automatically produced at Feb 27 2003, 22:51:56 by
% c2latex -c join1.c
%
\documentclass[11pt,a4paper]{article}
\setlength{\textwidth}{15cm}
\setlength{\textheight}{22.5cm}
\setlength{\hoffset}{-2cm}
\setlength{\voffset}{-2cm}
\begin{document}
\expandafter\ifx\csname indentation\endcsname\relax%
\newlength{\indentation}\fi
\setlength{\indentation}{0.5em}
\begin{flushleft}
{$/\ast$\it{}$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$\mbox{}\\
$\ast$ FILE: join1.c\mbox{}\\
$\ast$ DESCRIPTION:\mbox{}\\
$\ast$   This example demonstrates how to "wait" for thread completions by using\mbox{}\\
$\ast$   the Pthread join routine.  Since not all current implementations of\mbox{}\\
$\ast$   Pthreads create threads in a joinable state, the threads in this\mbox{}\\
$\ast$   example are explicitly created in a joinable state so that they can\mbox{}\\
$\ast$   be joined later.\mbox{}\\
$\ast$\mbox{}\\
$\ast$ SOURCE: 8/98 Blaise Barney\mbox{}\\
$\ast$ LAST REVISED:  10/8/99 Blaise Barney\mbox{}\\
$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast$$\ast/$}\mbox{}\\
{\tt \#include} $<${\tt{}pthread.h}$>$\mbox{}\\
{\tt \#include} $<${\tt{}stdio.h}$>$\mbox{}\\
{\tt \#define} NUM\_THREADS	3\mbox{}\\
\mbox{}\\
{\bf void} $\ast$BusyWork({\bf void} $\ast$null)\mbox{}\\
\{\mbox{}\\
\hspace*{3\indentation}{\bf int} i;\mbox{}\\
\hspace*{3\indentation}{\bf double} result=0.0;\mbox{}\\
\hspace*{3\indentation}{\bf for} (i=0; i$<$1000000; i++)\mbox{}\\
\hspace*{3\indentation}\{\mbox{}\\
\hspace*{5\indentation}result = result + ({\bf double})random();\mbox{}\\
\hspace*{3\indentation}\}\mbox{}\\
\hspace*{3\indentation}printf({\tt"Thread result $=$ \%e$\backslash$n"},result);\mbox{}\\
\hspace*{3\indentation}pthread\_exit(({\bf void} $\ast$) 0);\mbox{}\\
\}\mbox{}\\
\mbox{}\\
{\bf int} main({\bf int} argc, {\bf char} $\ast$argv[])\mbox{}\\
\{\mbox{}\\
\hspace*{3\indentation}pthread\_t thread[NUM\_THREADS];\mbox{}\\
\hspace*{3\indentation}pthread\_attr\_t attr;\mbox{}\\
\hspace*{3\indentation}{\bf int} rc, t, status;\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{} Initialize and set thread detached attribute $\ast/$}\mbox{}\\
\hspace*{3\indentation}pthread\_attr\_init(\&attr);\mbox{}\\
\hspace*{3\indentation}pthread\_attr\_setdetachstate(\&attr, PTHREAD\_CREATE\_JOINABLE);\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{\bf for}(t=0;t$<$NUM\_THREADS;t++)\mbox{}\\
\hspace*{3\indentation}\{\mbox{}\\
\hspace*{6\indentation}printf({\tt"Creating thread \%d$\backslash$n"}, t);\mbox{}\\
\hspace*{6\indentation}rc = pthread\_create(\&thread[t], \&attr, BusyWork, NULL); \mbox{}\\
\hspace*{6\indentation}{\bf if} (rc)\mbox{}\\
\hspace*{6\indentation}\{\mbox{}\\
\hspace*{9\indentation}printf({\tt"ERROR; return code from pthread\_create() is \%d$\backslash$n"}, rc);\mbox{}\\
\hspace*{9\indentation}exit($-$1);\mbox{}\\
\hspace*{6\indentation}\}\mbox{}\\
\hspace*{3\indentation}\}\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}{$/\ast$\it{} Free attribute and wait for the other threads $\ast/$}\mbox{}\\
\hspace*{3\indentation}pthread\_attr\_destroy(\&attr);\mbox{}\\
\hspace*{3\indentation}{\bf for}(t=0;t$<$NUM\_THREADS;t++)\mbox{}\\
\hspace*{3\indentation}\{\mbox{}\\
\hspace*{6\indentation}rc = pthread\_join(thread[t], ({\bf void} $\ast$$\ast$)\&status);\mbox{}\\
\hspace*{6\indentation}{\bf if} (rc)\mbox{}\\
\hspace*{6\indentation}\{\mbox{}\\
\hspace*{9\indentation}printf({\tt"ERROR return code from pthread\_join() is \%d$\backslash$n"}, rc);\mbox{}\\
\hspace*{9\indentation}exit($-$1);\mbox{}\\
\hspace*{6\indentation}\}\mbox{}\\
\hspace*{6\indentation}printf({\tt"Completed join with thread \%d status$=$ \%d$\backslash$n"},t, status);\mbox{}\\
\hspace*{3\indentation}\}\mbox{}\\
\mbox{}\\
\hspace*{3\indentation}pthread\_exit(NULL);\mbox{}\\
\}\mbox{}\\
\end{flushleft}
\end{document}
