|
|
<!DOCTYPE html>
|
|
|
<html lang="en-US" dir="ltr">
|
|
|
<head>
|
|
|
<meta charset="UTF-8">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
<meta name="description" content="Condition Variables # Condition variables allow a set of threads to sleep until woken up. The API allows either one or all threads to be woken up. If a program only wakes one thread, the operating system will decide which thread to wake up. Threads don’t wake threads other directly like by id. Instead, a thread ‘signal’s the condition variable, which then will wake up one (or all) threads that are sleeping inside the condition variable.">
|
|
|
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff">
|
|
|
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#343a40">
|
|
|
<meta name="color-scheme" content="light dark"><meta property="og:title" content="Condition Variables" />
|
|
|
<meta property="og:description" content="Condition Variables # Condition variables allow a set of threads to sleep until woken up. The API allows either one or all threads to be woken up. If a program only wakes one thread, the operating system will decide which thread to wake up. Threads don’t wake threads other directly like by id. Instead, a thread ‘signal’s the condition variable, which then will wake up one (or all) threads that are sleeping inside the condition variable." />
|
|
|
<meta property="og:type" content="article" />
|
|
|
<meta property="og:url" content="https://doc.cyub.vip/cs341/en/chap7/Condition_Variables/" /><meta property="article:section" content="chap7" />
|
|
|
|
|
|
|
|
|
<title>Condition Variables | System Programming</title>
|
|
|
<link rel="manifest" href="/cs341/manifest.json">
|
|
|
<link rel="icon" href="/cs341/favicon.png" >
|
|
|
<link rel="stylesheet" href="/cs341/book.min.0356a007b16d20385fd8a4fa025ec8be3122d726acbce111d6af7545fd9a6d9a.css" integrity="sha256-A1agB7FtIDhf2KT6Al7IvjEi1yasvOER1q91Rf2abZo=" crossorigin="anonymous">
|
|
|
<script defer src="/cs341/flexsearch.min.js"></script>
|
|
|
<script defer src="/cs341/en.search.min.25e65b45853c7470253af707592ba1bc73827661c6424245ee2246bfa49316c8.js" integrity="sha256-JeZbRYU8dHAlOvcHWSuhvHOCdmHGQkJF7iJGv6STFsg=" crossorigin="anonymous"></script>
|
|
|
|
|
|
<script defer src="/cs341/sw.min.26457b123904e37c0084a63054d7f37a2a820ad60cda19209104bfc0b2c496b0.js" integrity="sha256-JkV7EjkE43wAhKYwVNfzeiqCCtYM2hkgkQS/wLLElrA=" crossorigin="anonymous"></script>
|
|
|
<!--
|
|
|
Made with Book Theme
|
|
|
https://github.com/alex-shpak/hugo-book
|
|
|
-->
|
|
|
|
|
|
</head>
|
|
|
<body dir="ltr">
|
|
|
<input type="checkbox" class="hidden toggle" id="menu-control" />
|
|
|
<input type="checkbox" class="hidden toggle" id="toc-control" />
|
|
|
<main class="container flex">
|
|
|
<aside class="book-menu">
|
|
|
<div class="book-menu-content">
|
|
|
|
|
|
<nav>
|
|
|
<h2 class="book-brand">
|
|
|
<a class="flex align-center" href="/cs341/en/"><span>System Programming</span>
|
|
|
</a>
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
<div class="book-search">
|
|
|
<input type="text" id="book-search-input" placeholder="Search" aria-label="Search" maxlength="64" data-hotkeys="s/" />
|
|
|
<div class="book-search-spinner hidden"></div>
|
|
|
<ul id="book-search-results"></ul>
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul class="book-languages">
|
|
|
<li>
|
|
|
<input type="checkbox" id="languages" class="toggle" />
|
|
|
<label for="languages" class="flex justify-between">
|
|
|
<a role="button" class="flex align-center">
|
|
|
<img src="/cs341/svg/translate.svg" class="book-icon" alt="Languages" />
|
|
|
English
|
|
|
</a>
|
|
|
</label>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
<li>
|
|
|
<a href="https://doc.cyub.vip/cs341/cn/">
|
|
|
chinese
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
</li>
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap1/">1. Introduction</a></strong></p>
|
|
|
<ul>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap1/Authors/">Authors</a></li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap2/">2. Background</a></strong></p>
|
|
|
<ul>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap2/Systems_Architecture/">Systems Architecture</a></li>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap2/Debugging_Environments/">Debugging and Environments</a></li>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap2/Valgrind/">Valgrind</a></li>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap2/GDB/">GDB</a></li>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap2/Homework0/">Homework 0</a></li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap3/">3. The C Programming Language</a></strong></p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap4/">4. Processes</a></strong></p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap5/">5. Memory Allocators</a></strong></p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap6/">6. Threads</a></strong></p>
|
|
|
</li>
|
|
|
<li>
|
|
|
<p><strong>
|
|
|
<a href="/cs341/en/chap7/">7. Synchronization</a></strong></p>
|
|
|
<ul>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap7/Mutex/">Mutex</a></li>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap7/Semaphore/">Semaphore</a></li>
|
|
|
<li>
|
|
|
<a href="/cs341/en/chap7/Condition_Variables/"class=active>Condition Variables</a></li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</aside>
|
|
|
|
|
|
<div class="book-page">
|
|
|
<header class="book-header">
|
|
|
|
|
|
<div class="flex align-center justify-between">
|
|
|
<label for="menu-control">
|
|
|
<img src="/cs341/svg/menu.svg" class="book-icon" alt="Menu" />
|
|
|
</label>
|
|
|
|
|
|
<strong>Condition Variables</strong>
|
|
|
|
|
|
<label for="toc-control">
|
|
|
|
|
|
</label>
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
|
|
<article class="markdown"><h1 id="condition-variables">
|
|
|
Condition Variables
|
|
|
<a class="anchor" href="#condition-variables">#</a>
|
|
|
</h1>
|
|
|
<p><strong>Condition variables</strong> allow a set of threads to sleep until woken up. The API allows either one or all threads to be woken up. If a program only wakes one thread, the operating system will decide which thread to wake up. Threads don’t wake threads other directly like by id. Instead, a thread ‘signal’s the condition variable, which then will wake up one (or all) threads that are sleeping inside the condition variable.</p>
|
|
|
<p>Condition variables are also used with a mutex and with a loop, so when woken up they have to check a condition in a critical section. If a thread needs to be woken up outside of a critical section, there are other ways to do this in POSIX. Threads sleeping inside a condition variable are woken up by calling <code>pthread_cond_broadcast</code> (wake up all) or <code>pthread_cond_signal</code> (wake up one). Note despite the function name, this has nothing to do with POSIX <code>signals</code>!</p>
|
|
|
<p>Occasionally, a waiting thread may appear to wake up for no reason. This is called a spurious wakeup. If you read the hardware implementation of a mutex section, this is similar to the atomic failure of the same name.</p>
|
|
|
<p>Why do spurious wakeups happen? For performance. On multi-CPU systems, it is possible that a race condition could cause a wake-up (signal) request to be unnoticed. The kernel may not detect this lost wake-up call but can detect when it might occur. To avoid the potentially lost signal, the thread is woken up so that the program code can test the condition again. If you want to know why, check out the appendix.</p>
|
|
|
</article>
|
|
|
|
|
|
|
|
|
|
|
|
<footer class="book-footer">
|
|
|
|
|
|
<div class="flex flex-wrap justify-between">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</footer>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="book-comments">
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<label for="menu-control" class="hidden book-menu-overlay"></label>
|
|
|
</div>
|
|
|
|
|
|
|
|
|
</main>
|
|
|
|
|
|
|
|
|
</body>
|
|
|
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|