diff --git "a/docs/concurrence/3_Mutex_et_S\303\251maphore/1_mutex_semaphore.md" "b/docs/concurrence/3_Mutex_et_S\303\251maphore/1_mutex_semaphore.md" new file mode 100644 index 0000000..34db8d0 --- /dev/null +++ "b/docs/concurrence/3_Mutex_et_S\303\251maphore/1_mutex_semaphore.md" @@ -0,0 +1,9 @@ +--- +hide_table_of_contents: true +hide_title: true +title: Concurrence et parallélisme +--- + +
+ +
diff --git "a/docs/concurrence/3_Mutex_et_S\303\251maphore/2_code.md" "b/docs/concurrence/3_Mutex_et_S\303\251maphore/2_code.md" new file mode 100644 index 0000000..0b1925b --- /dev/null +++ "b/docs/concurrence/3_Mutex_et_S\303\251maphore/2_code.md" @@ -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'; + + + + +```c +#include +#include + +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` + + + + +```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` + + +