diff --git a/slides/images/mutex.png b/slides/images/mutex.png new file mode 100644 index 0000000000000000000000000000000000000000..c2012447540bfa23f460ddf65826fc7f8790da6c Binary files /dev/null and b/slides/images/mutex.png differ diff --git a/slides/pages/qa.md b/slides/pages/qa.md index cc738c04d9da67b49f1c3c4f08a948d25851722c..7984bf6e6908265666ea799d13a5f8cd221e09a6 100644 --- a/slides/pages/qa.md +++ b/slides/pages/qa.md @@ -4,7 +4,7 @@ title: Q&A # Q&A -Questions about: +Any questions about: - Second Assignment Sheet - Third Assignment Sheet @@ -12,3 +12,9 @@ Questions about: - Organisation <br/> + +### Materials + +- [Concurrency vs Parallelism](https://freecontent.manning.com/concurrency-vs-parallelism/) +- [pthreads(7)](https://man7.org/linux/man-pages/man7/pthreads.7.html) +- [Makefile Tutorial](https://makefiletutorial.com/#getting-started) diff --git a/slides/pages/recap.md b/slides/pages/recap.md index 182306c75525eb4d10cb4d4e22e84b88031cfe27..fa8f6fdf620cf45dd13d6490cd0eab89e3cde60e 100644 --- a/slides/pages/recap.md +++ b/slides/pages/recap.md @@ -162,3 +162,76 @@ int pthread_detach(pthread_t thread); // A thread can detach itself in the start_routine pthread_detach(pthread_self()); ``` + +--- +title: Recap VI +layout: two-cols +--- + +### Thread Synchronization + +To avoid race conditions, we must use a _mutex_(short for mutual exclusion) to ensure that only one thread a time can access the shared data. + +A mutex has two states: _locked_ and _unlocked_. + +When a thread locks a mutex, it becomes the owner of that mutex. Only the mutex owner can unlock the mutex. + +**In general, we employ a different mutex for each shared resource.** + +::right:: + +<div class="container flex justify-center mt-10"> + <img src="/images/mutex.png" class="block w-sm"/> +</div> + + +--- +title: Recap VII +--- + +#### Statically Allocated Mutexes + +A mutex can either be allocated as a static variable or be created dynamically at run time (for example using `malloc()`). + +A mutex is a variable of the type `pthread_mutex_t`. Before it can be used, a mutex must always be initialized. + +```c +pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; +``` + +<br/> + +#### Dynamically Initializing a Mutex + +The static initializer value `PTHREAD_MUTEX_INITIALIZER` can be used only for initializing a statically allocated mutex with default attributes. + +```c +int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); +``` + +--- +title: Recap VIII +--- + +#### Locking and Unlocking a Mutex + +After initialization, a mutex is unlocked. You can use the following functions to lock and unlock the mutex: + +```c +int pthread_mutex_lock(pthread_mutex_t *mutex); +int pthread_mutex_unlock(pthread_mutex_t *mutex); +``` + +**They always appear in pairs.** + +#### Destroying a Mutex + +When an automatically or dynamically allocated mutex is no longer required, it should be destroyed using `pthread_mutex_destroy()`. + +```c +int pthread_mutex_destroy(pthread_mutex_t *mutex); +``` + +**Note:** It is not necessary to call `pthread_mutex_destroy()` on a mutex that was statically initialized using `PTHREAD_MUTEX_INITIALIZER`. + +