Skip to content

Commit

Permalink
U
Browse files Browse the repository at this point in the history
  • Loading branch information
fortierq committed Sep 1, 2024
1 parent 146d281 commit 58f4fb0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/concurrence/3_Mutex_et_Sémaphore/1_mutex_semaphore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
hide_table_of_contents: true
hide_title: true
title: Concurrence et parallélisme
---

<div class="container4x3">
<iframe src={require('./mutex_semaphore.pdf#zoom=page-fit&pagemode=none').default + "#zoom=page-fit&pagemode=none"} class="responsive-iframe" allowFullScreen></iframe>
</div>
72 changes: 72 additions & 0 deletions docs/concurrence/3_Mutex_et_Sémaphore/2_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
hide_table_of_contents: false
hide_title: true
title: Code
---

## Mutex

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs>
<TabItem value="c" label="C">

```c
#include <stdio.h>
#include <pthread.h>

int counter;
pthread_mutex_t mutex;

void *increment(void *arg){
for (int i = 1; i <= 1000000; i++) {
pthread_mutex_lock(&mutex);
counter++;
pthread_mutex_unlock(&mutex);
}
return NULL;
}

int main(void){
counter = 0;
pthread_t t1, t2;
pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Counter = %d\n", counter);
return 0;
}
```
Compilation : `gcc -pthread exemple.c`
</TabItem>
<TabItem value="OCaml" label="OCaml">
```ocaml
let counter = ref 0
let lock = Mutex.create ()
let multiple_increment n =
for i = 0 to n - 1 do
Mutex.lock lock;
counter := !counter + 1;
Mutex.unlock lock
done
let main () =
let n = 1_000_000 in
let t0 = Thread.create multiple_increment n in
let t1 = Thread.create multiple_increment n in
Thread.join t0;
Thread.join t1;
Printf.printf "counter = %d\n" !counter
let () = main ()
```

Compilation : `ocamlopt -I +unix -I +threads unix.cmxa threads.cmxa exemple.ml`

</TabItem>
</Tabs>

0 comments on commit 58f4fb0

Please sign in to comment.