os_cp:threads
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| os_cp:threads [2025/04/22 22:05] – [Critical sections, deadlocks] jkonczak | os_cp:threads [2025/04/25 11:55] (current) – [POSIX condition variables] jkonczak | ||
|---|---|---|---|
| Line 704: | Line 704: | ||
| ===== Critical sections, deadlocks ===== | ===== Critical sections, deadlocks ===== | ||
| - | ~~Exercise.# | + | ~~Exercise.# |
| ~~Exercise.# | ~~Exercise.# | ||
| Line 712: | Line 712: | ||
| < | < | ||
| <code c> | <code c> | ||
| - | ⋯ | + | … |
| - | 9│ #define NB_THR 5 | + | 10│ struct myIO {FILE *in; FILE *out;}; |
| - | 10│ struct myIO {FILE *in; FILE *out;}; | + | 11│ void* threadFunc(void *num); |
| - | 11│ void* threadFunc(void *num); | + | … |
| - | ⋯ | + | 17│ struct { |
| - | 17│ struct { | + | 18│ |
| - | | + | 19│ char text[256]; |
| - | | + | 20│ } item[5]; |
| - | 20│ } item[5]; | + | 21│ |
| - | 21│ | + | 22│ int main(int argc, char **argv) { |
| - | 22│ int main(int argc, char **argv) { | + | … |
| - | ⋯ | + | 28│ for (intptr_t i = 1; i < 5; ++i) { |
| - | | + | 29│ |
| - | | + | 30│ |
| - | | + | 31│ |
| - | | + | 32│ } |
| - | | + | 33│ |
| - | | + | 34│ } |
| - | 34│ } | + | 35│ |
| - | 35│ | + | 36│ void *threadFunc(void* numRaw) { |
| - | 36│ void *threadFunc(void* numRaw) { | + | 37│ |
| - | | + | 38│ |
| - | | + | … |
| - | ⋯ | + | 48│ while (1) { |
| - | 49│ while (1) { | + | 49│ |
| - | | + | 50│ |
| - | 51│ fprintf(win.out, "> "); | + | 51│ char line[1024]; |
| - | | + | 52│ fgets(line, 1024, win.in); |
| - | | + | 53│ line[strlen(line)-1] = 0; |
| - | | + | 54│ |
| - | ⋯ | + | 55│ int argOne = atoi(line); |
| - | 62│ char *argTwoTxt = strchr(line, | + | … |
| - | 63│ | + | 61│ char *argTwoTxt = strchr(line, |
| - | 64│ if (!argTwoTxt) { | + | 62│ |
| - | 66│ | + | 63│ if (!argTwoTxt) { |
| - | 67│ | + | 65│ |
| - | 69│ | + | 66│ |
| - | 70│ | + | 67│ |
| - | 71│ } | + | 68│ |
| - | 72│ | + | 69│ } |
| - | 73│ | + | 70│ |
| - | 74│ char *e; | + | 71│ |
| - | 75│ int argTwo = strtol(argTwoTxt, | + | 72│ char *e; |
| - | 76│ | + | 73│ int argTwo = strtol(argTwoTxt, |
| - | 77│ if (!*e && argTwo < 5 && argTwo >= 0 && argOne != argTwo) { | + | 74│ |
| - | 79│ | + | 75│ if (!*e && argTwo < 5 && argTwo >= 0 && argOne != argTwo) { |
| - | 80│ | + | 77│ |
| - | 81│ | + | 78│ |
| - | 83│ | + | 79│ |
| - | 84│ | + | 80│ |
| - | 85│ | + | 81│ |
| - | 86│ } else { | + | 82│ |
| - | 88│ | + | 83│ } else { |
| - | 89│ | + | 85│ |
| - | 91│ | + | 86│ |
| - | 92│ | + | 87│ |
| - | 93│ | + | 88│ |
| - | 94│ } | + | 89│ |
| - | 95│ } | + | 90│ } |
| - | 96│ } | + | 91│ } |
| - | ⋯ | + | 92│ } |
| + | … | ||
| </ | </ | ||
| < | < | ||
| Line 876: | 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.1745359551.txt.gz · Last modified: (external edit)