Skip to content
Snippets Groups Projects
Commit 8e2f9358 authored by Mactavish's avatar Mactavish
Browse files

update slides

parent 466a6acd
No related branches found
No related tags found
No related merge requests found
slides/images/mutex.png

44.9 KiB

...@@ -4,7 +4,7 @@ title: Q&A ...@@ -4,7 +4,7 @@ title: Q&A
# Q&A # Q&A
Questions about: Any questions about:
- Second Assignment Sheet - Second Assignment Sheet
- Third Assignment Sheet - Third Assignment Sheet
...@@ -12,3 +12,9 @@ Questions about: ...@@ -12,3 +12,9 @@ Questions about:
- Organisation - Organisation
<br/> <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)
...@@ -162,3 +162,76 @@ int pthread_detach(pthread_t thread); ...@@ -162,3 +162,76 @@ int pthread_detach(pthread_t thread);
// A thread can detach itself in the start_routine // A thread can detach itself in the start_routine
pthread_detach(pthread_self()); 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`.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment