Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP4-Tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mactavish96
ALP4-Tutorials
Commits
8e2f9358
Commit
8e2f9358
authored
2 years ago
by
Mactavish
Browse files
Options
Downloads
Patches
Plain Diff
update slides
parent
466a6acd
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
slides/images/mutex.png
+0
-0
0 additions, 0 deletions
slides/images/mutex.png
slides/pages/qa.md
+7
-1
7 additions, 1 deletion
slides/pages/qa.md
slides/pages/recap.md
+73
-0
73 additions, 0 deletions
slides/pages/recap.md
with
80 additions
and
1 deletion
slides/images/mutex.png
0 → 100644
+
0
−
0
View file @
8e2f9358
44.9 KiB
This diff is collapsed.
Click to expand it.
slides/pages/qa.md
+
7
−
1
View file @
8e2f9358
...
@@ -4,7 +4,7 @@ title: Q&A
...
@@ -4,7 +4,7 @@ title: Q&A
# Q&A
# Q&A
Q
uestions about:
Any q
uestions 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
)
This diff is collapsed.
Click to expand it.
slides/pages/recap.md
+
73
−
0
View file @
8e2f9358
...
@@ -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`
.
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment