os_cp:threads
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| os_cp:threads [2025/04/25 11:11] – [Critical sections, deadlocks] jkonczak | os_cp:threads [2025/04/25 11:55] (current) – [POSIX condition variables] jkonczak | ||
|---|---|---|---|
| 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: | ||
| + | < | ||
| + | <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 half column> | ||
| + | < | ||
| + | <pre class=" | ||
| + | <span style=" | ||
| + | <span class=" | ||
| + | <span style=" | ||
| + | < | ||
| + | <span style=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| 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: | ||
| + | < | ||
| + | <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 half column> | ||
| + | < | ||
| + | <pre class=" | ||
| + | <span style=" | ||
| + | < | ||
| + | <span style=" | ||
| + | <span style=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| < | < | ||
| The example conditions include: | The example conditions include: | ||
| + | < | ||
| * 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, | ||
os_cp/threads.1745579499.txt.gz · Last modified: (external edit)