This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
os_cp:threads [2025/04/25 13:11] jkonczak [Critical sections, deadlocks] |
os_cp:threads [2025/04/25 13:55] (current) jkonczak [POSIX condition variables] |
||
---|---|---|---|
Line 877: | Line 877: | ||
When one thread needs to execute some logic once a specific condition is fulfilled, | When one thread needs to execute some logic once a specific condition is fulfilled, | ||
it should: | it should: | ||
+ | <html><div style="margin-top:-1.2em"></div></html> | ||
+ | <WRAP group> | ||
+ | <WRAP half column> | ||
- lock a mutex, | - lock a mutex, | ||
- check the condition, | - check the condition, | ||
- | - if the condition is false: | + | - while the condition is false: |
- wait on a condition variable, | - wait on a condition variable, | ||
- go back to step 2, | - go back to step 2, | ||
- do the logic, | - do the logic, | ||
- unlock the mutex. | - unlock the mutex. | ||
+ | </WRAP> | ||
+ | <WRAP half column> | ||
+ | <html> | ||
+ | <pre class="code c" style="margin-top:-1.4em;margin-bottom:-1.4em;"> | ||
+ | <span style="opacity:0.66">pthread_mutex_</span>lock<span class="br0">(</span><span class="sy0">&</span>mutex<span class="br0">)</span><span class="sy0">;</span> | ||
+ | <span class="kw1">while</span><span class="br0">(</span><span class="sy0">!</span><i>condition</i><span class="br0">)</span> | ||
+ | <span style="opacity:0.66">pthread_cond_</span>wait<span class="br0">(</span><span class="sy0">&</span>condvar<span class="sy0">,</span> <span class="sy0">&</span>mutex<span class="br0">)</span><span class="sy0">;</span> | ||
+ | <i>logic<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></i> | ||
+ | <span style="opacity:0.66">pthread_mutex_</span>unlock<span class="br0">(</span><span class="sy0">&</span>mutex<span class="br0">)</span><span class="sy0">;</span> | ||
+ | </pre> | ||
+ | </html> | ||
+ | </WRAP> | ||
+ | </WRAP> | ||
A thread that may change the state and thus affect the condition should: | A thread that may change the state and thus affect the condition should: | ||
+ | <html><div style="margin-top:-1.2em"></div></html> | ||
+ | <WRAP group> | ||
+ | <WRAP half column> | ||
- lock the mutex, | - lock the mutex, | ||
- do its logic, | - do its logic, | ||
- signal the condition variable, | - signal the condition variable, | ||
- unlock the mutex((One can also signal the condition variable after unlocking the mutex.)). | - unlock the mutex((One can also signal the condition variable after unlocking the mutex.)). | ||
+ | </WRAP> | ||
+ | <WRAP half column> | ||
+ | <html> | ||
+ | <pre class="code c" style="padding-top:0; padding-bottom:0; margin-top:-1.4em;margin-bottom:-1.4em;"> | ||
+ | <span style="opacity:0.66">pthread_mutex_</span>lock<span class="br0">(</span><span class="sy0">&</span>mutex<span class="br0">)</span><span class="sy0">;</span> | ||
+ | <i>logic_that_changes_condition<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></i> | ||
+ | <span style="opacity:0.66">pthread_cond_</span>signal<span class="br0">(</span><span class="sy0">&</span>condvar<span class="br0">)</span><span class="sy0">;</span> | ||
+ | <span style="opacity:0.66">pthread_mutex_</span>unlock<span class="br0">(</span><span class="sy0">&</span>mutex<span class="br0">)</span><span class="sy0">;</span> | ||
+ | </pre> | ||
+ | </html> | ||
+ | </WRAP> | ||
+ | </WRAP> | ||
<small> | <small> | ||
The example conditions include: | The example conditions include: | ||
+ | <html><div style="margin-top:-1.2em"></div></html> | ||
* a boolean flag is set; the flag indicates that another thread finished a part of the computation and the results can used, | * a boolean flag is set; the flag indicates that another thread finished a part of the computation and the results can used, | ||
* a list of items is not empty; the list contains tasks to be done by this thread, | * a list of items is not empty; the list contains tasks to be done by this thread, |