-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpthread-Tutorial.tex
More file actions
130 lines (106 loc) · 5.49 KB
/
pthread-Tutorial.tex
File metadata and controls
130 lines (106 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FILE : pthread-Tutorial.tex
% AUTHOR : (C) Copyright 2020 by Peter C. Chapin
% SUBJECT : Tutorial on POSIX threads.
%
% This document attempts to give a quick overview of how to use the facilities of POSIX threads.
% It is not intended to be a complete description of pthreads. Instead I only intend to
% introduce the reader to basic pthread operations. The hope is that there will be enough
% information here to allow the reader to start using pthreads effectively without overwhelming
% the reader with details.
%
% TODO:
%
% + Add some kind of reference or bibliography section.
%
% + Add some kind of glossary and/or list of abbreviations.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%+++++++++++++++++++++++++++++++++
% Preamble and global declarations
%+++++++++++++++++++++++++++++++++
\documentclass{article}
\usepackage{listings}
%\usepackage[dvips]{graphics}
\usepackage{hyperref}
\setlength{\parindent}{0em}
\setlength{\parskip}{1.75ex plus0.5ex minus0.5ex}
% I want to typeset newly introduced terms in a special way.
\newcommand{\newterm}[1]{\emph{#1}}
% Used for bits of C in-line.
\newcommand{\snippet}[1]{\texttt{#1}}
%++++++++++++++++++++
% The document itself
%++++++++++++++++++++
\begin{document}
% The following are settings for the listings package.
\lstset{language=C,
basicstyle=\small,
stringstyle=\ttfamily,
commentstyle=\ttfamily,
xleftmargin=0.25in,
showstringspaces=false}
%-----------------------
% Title page information
%-----------------------
\title{pthread Tutorial}
\author{\copyright\ Copyright 2020 by Peter C. Chapin}
\date{January 18, 2020}
\maketitle
\tableofcontents
\section*{Legal}
\label{sec:legal}
\textit{Permission is granted to copy, distribute and/or modify this document under the terms of
the GNU Free Documentation License, Version 1.1 or any later version published by the Free
Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no
Back-Cover Texts. A copy of the license is included in the file \texttt{GFDL.txt} distributed
with the \LaTeX\ source of this document.}
\section{Introduction}
\label{sec:intro}
This document is intended to be a short but useful tutorial on how to use POSIX threads
(pthreads). In this document I do not attempt to give a full description of all pthread
features. Instead I hope to give you enough information to use pthreads in a basic, yet
effective way. Please refer to a text on pthreads for the more esoteric details of the standard.
In addition to talking about the pthread interface itself, I also spend time in this document
discussing issues regarding concurrent programming in general. While such issues are not
specific to pthreads, it is a must that you understand them if you are to use pthreads---or any
thread library---effectively.
I will assume that you are compiling pthreads programs on a Unix system. However, you should be
aware that the pthreads interface is not necessarily specific to Unix. It is a standard
application program interface that could potentially be implemented on many different systems.
However, pthreads is the usual way multi-threaded support is offered in the Unix world. Although
many systems support their own internal method of handling threads, virtually every Unix system
that supports threads at all offers the pthreads interface.
The pthreads API can be implemented either in the kernel of the operating system or in a
library. It can either be preemptive or it can be non-preemptive. A portable program based on
pthreads should not make any assumptions about these matters.
When you compile a program that uses pthreads, you may have to set special options on the
compiler's command line to indicate extra (or different) libraries and/or alternative code
generating stratagies. Consult your compiler's documentation for more information on this. Often
you can indicate your desire to use pthreads by supplying the ``-pthread'' option at the end of
the compiler command line. For example
\begin{verbatim}
$ gcc -o myprog myprog.c -pthread
\end{verbatim}
This single option specifies that the pthreads library should be linked and also causes the
compiler to properly handle the multiple threads in the code that it generates.
\input{create-destroy.tex}
\input{sync.tex}
\input{models.tex}
\input{safety.tex}
\input{rules.tex}
\section{Conclusion}
\label{sec:conclusion}
Using multiple threads has the potential to improve your program's performance. Even on a single
processor system, performance might be increased because one thread can execute while another is
blocked waiting on I/O (for example). Also, used appropriately, multiple threads can allow for
some elegant designs that can clarify and simplify the architecture of your system.
The pthreads API is a relatively low level API that offers the usual features of such APIs: it
is flexible and powerful, but also verbose and hard to use properly. Higher level thread APIs
exist, often built on top of the pthreads API, that are easier to use correctly but sacrifice
some of the flexibility and generality of pthreads.
This document was prepared to support my classes at Vermont Technical College. However, I offer
it to the public for general consumption on the hope that others might find it useful. Please
don't hesitate to send me corrections or comments or suggestions for improvements or new
sections. I can be reached at \href{mailto:pchapin@vtc.edu}{pchapin@vtc.edu}.
Peter Chapin
\end{document}