Changeset 9363b1b for doc/theses/colby_parsons_MMAth/text/mutex_stmt.tex
- Timestamp:
- Apr 4, 2023, 9:50:07 AM (18 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 59c05958
- Parents:
- e8b1f23c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/colby_parsons_MMAth/text/mutex_stmt.tex
re8b1f23c r9363b1b 12 12 Additionally, it provides the safety guarantee of deadlock-freedom, both by acquiring the locks in a deadlock-free manner, and by ensuring that the locks release on error, or normal program execution via \gls{raii}. 13 13 14 \begin{cfa code}[tabsize=3,caption={\CFA mutex statement usage},label={l:cfa_mutex_ex}]14 \begin{cfa}[tabsize=3,caption={\CFA mutex statement usage},label={l:cfa_mutex_ex}] 15 15 owner_lock lock1, lock2, lock3; 16 16 int count = 0; … … 20 20 } 21 21 mutex( lock2, lock3 ) count++; // or inline statement 22 \end{cfa code}22 \end{cfa} 23 23 24 24 \section{Other Languages} … … 32 32 An example of \CC scoped\_lock usage is shown in Listing~\ref{l:cc_scoped_lock}. 33 33 34 \begin{c ppcode}[tabsize=3,caption={\CC scoped\_lock usage},label={l:cc_scoped_lock}]34 \begin{cfa}[tabsize=3,caption={\CC scoped\_lock usage},label={l:cc_scoped_lock}] 35 35 std::mutex lock1, lock2, lock3; 36 36 { … … 38 38 // locks are released via raii at end of scope 39 39 } 40 \end{c ppcode}40 \end{cfa} 41 41 42 42 \section{\CFA implementation} … … 58 58 This use case is shown in Listing~\ref{l:sout}. 59 59 60 \begin{cfa code}[tabsize=3,caption={\CFA sout with mutex statement},label={l:sout}]60 \begin{cfa}[tabsize=3,caption={\CFA sout with mutex statement},label={l:sout}] 61 61 mutex( sout ) 62 62 sout | "This output is protected by mutual exclusion!"; 63 \end{cfa code}63 \end{cfa} 64 64 65 65 \section{Deadlock Avoidance} … … 70 70 The algorithm presented is taken directly from the source code of the \code{<mutex>} header, with some renaming and comments for clarity. 71 71 72 \begin{c ppcode}[caption={\CC scoped\_lock deadlock avoidance algorithm},label={l:cc_deadlock_avoid}]72 \begin{cfa}[caption={\CC scoped\_lock deadlock avoidance algorithm},label={l:cc_deadlock_avoid}] 73 73 int first = 0; // first lock to attempt to lock 74 74 do { … … 86 86 // if first lock is still held then all have been acquired 87 87 } while (!locks[first].owns_lock()); // is first lock held? 88 \end{c ppcode}88 \end{cfa} 89 89 90 90 The algorithm in \ref{l:cc_deadlock_avoid} successfully avoids deadlock, however there is a potential livelock scenario.
Note: See TracChangeset
for help on using the changeset viewer.