Skip to content

Commit fe3e501

Browse files
committed
move pthread_create() slide up
so that notes with API examples make sense
1 parent 85bd75e commit fe3e501

File tree

1 file changed

+55
-54
lines changed

1 file changed

+55
-54
lines changed

threads.tex

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -160,60 +160,6 @@
160160
functions.
161161
\item We will talk more on reentrant functions in connection with threads on
162162
page \pageref{THREADSAFE}.
163-
\item As already mentioned, given that the POSIX thread API uses \texttt{errno}
164-
codes directly as return values, the following piece of code is not correct:
165-
166-
\begin{verbatim}
167-
if (pthread_create(&thr, NULL, thrfn, NULL) != 0)
168-
err(1, "pthread_create");
169-
\end{verbatim}
170-
171-
as \texttt{err()} will print possibly something like the following on error
172-
(unless \texttt{errno} was set by previous code which would make it even more
173-
confusing):
174-
175-
\begin{itemize}
176-
\item ``\texttt{a.out: pthread\_create: Success}'' on Linux distributions
177-
\item ``\texttt{a.out: pthread\_create: Error 0}'' on Solaris
178-
\item ``\texttt{a.out: pthread\_create: Unknown error: 0}'' on FreeBSD
179-
\item or something else based on the system and the concrete message it uses for
180-
\texttt{errno} equal to 0, unless \texttt{errno} is already set otherwise.
181-
\end{itemize}
182-
183-
The Linux approach might confuse the programmer as leaving \texttt{errno} zero
184-
does not have to mean the function did not fail, as we just showed. FreeBSD
185-
makes it obvious that something is not entirely right. Example that shows such
186-
a situation: \example{pthreads/wrong-err-use.c}. The correct code could look
187-
like this:
188-
189-
\begin{verbatim}
190-
int e;
191-
192-
if ((e = pthread_create(&thr, NULL, thrfn, NULL)) != 0)
193-
errx(1, "pthread_create: %s", strerror(e));
194-
\end{verbatim}
195-
196-
\item \hlabel{ERRNO_IN_THREADS} Other functions that use \texttt{errno} work the
197-
same with POSIX threads as each thread has its own \texttt{errno}. In that
198-
case, it is redefined using a function (which can either return the value or an
199-
address which is dereferenced). Check \texttt{/usr/include/errno.h} on Linux if
200-
interested.
201-
\end{itemize}
202-
203-
%%%%%
204-
205-
\begin{slide}
206-
\sltitle{Example: thread creation}
207-
{\catcode95=12\catcode38=12
208-
\begin{center}
209-
\input{img/tex/threads.pstex_t}
210-
\end{center}}
211-
\end{slide}
212-
213-
\begin{itemize}
214-
\item This is a trivial example. The process (main thread) creates two more
215-
threads and waits for them to finish. This process thus has 3 threads in
216-
total.
217163
\end{itemize}
218164

219165
%%%%%
@@ -288,6 +234,61 @@
288234
threads, you need to pass a different address for a \texttt{pthread\_t} variable
289235
otherwise you will not be able to further manipulate with the threads from the
290236
main thread, e.g. waiting for them to finish.
237+
\item As already mentioned, given that the POSIX thread API uses \texttt{errno}
238+
codes directly as return values, the following piece of code is not correct:
239+
240+
\begin{verbatim}
241+
if (pthread_create(&thr, NULL, thrfn, NULL) != 0)
242+
err(1, "pthread_create");
243+
\end{verbatim}
244+
245+
as \texttt{err()} will print possibly something like the following on error
246+
(unless \texttt{errno} was set by previous code which would make it even more
247+
confusing):
248+
249+
\begin{itemize}
250+
\item ``\texttt{a.out: pthread\_create: Success}'' on Linux distributions
251+
\item ``\texttt{a.out: pthread\_create: Error 0}'' on Solaris
252+
\item ``\texttt{a.out: pthread\_create: Unknown error: 0}'' on FreeBSD
253+
\item or something else based on the system and the concrete message it uses for
254+
\texttt{errno} equal to 0, unless \texttt{errno} is already set otherwise.
255+
\end{itemize}
256+
257+
The Linux approach might confuse the programmer as leaving \texttt{errno} zero
258+
does not have to mean the function did not fail, as we just showed. FreeBSD
259+
makes it obvious that something is not entirely right. Example that shows such
260+
a situation: \example{pthreads/wrong-err-use.c}. The correct code could look
261+
like this:
262+
263+
\begin{verbatim}
264+
int e;
265+
266+
if ((e = pthread_create(&thr, NULL, thrfn, NULL)) != 0)
267+
errx(1, "pthread_create: %s", strerror(e));
268+
\end{verbatim}
269+
270+
\item \hlabel{ERRNO_IN_THREADS} Other functions that use \texttt{errno} work the
271+
same with POSIX threads as each thread has its own \texttt{errno}. In that
272+
case, it is redefined using a function (which can either return the value or an
273+
address which is dereferenced). Check \texttt{/usr/include/errno.h} on Linux if
274+
interested.
275+
276+
\end{itemize}
277+
278+
%%%%%
279+
280+
\begin{slide}
281+
\sltitle{Example: thread creation}
282+
{\catcode95=12\catcode38=12
283+
\begin{center}
284+
\input{img/tex/threads.pstex_t}
285+
\end{center}}
286+
\end{slide}
287+
288+
\begin{itemize}
289+
\item This is a trivial example. The process (main thread) creates two more
290+
threads and waits for them to finish. This process thus has 3 threads in
291+
total.
291292
\end{itemize}
292293

293294
%%%%%

0 commit comments

Comments
 (0)