add condition variable

main
tink 2 years ago
parent 9711f6c0fd
commit 15ab3a8077

@ -1,7 +1,12 @@
HTML_OUTPUT = /var/www/illinois-cs341-system-programming
all:
serve:
hugo serve
html:
hugo
hugo
publish:
ssh root@www.cyub.vip "cd ${HTML_OUTPUT};git pull"

@ -0,0 +1,13 @@
+++
title = "Condition Variables"
+++
# 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 dont wake threads other directly like by id. Instead, a thread signals the condition variable, which then will wake up one (or all) threads that are sleeping inside the condition variable.
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 `pthread_cond_broadcast` (wake up all) or `pthread_cond_signal` (wake up one). Note despite the function name, this has nothing to do with POSIX `signals`!
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.
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.

@ -0,0 +1,7 @@
+++
title = "Synchronization"
+++
# Synchronization
When multithreading gets interesting

@ -20,4 +20,7 @@ headless: true
- **[5. Memory Allocators]({{< relref "/chap5" >}})**
- **[6. Threads]({{< relref "/chap6" >}})**
- **[7. Synchronization]({{< relref "/chap7" >}})**
- [Condition Variables]({{< relref "/chap7/Condition_Variables" >}})

Loading…
Cancel
Save