Skip to content

Commit

Permalink
slides
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jan 4, 2022
1 parent 7fa7c9d commit 28af775
Show file tree
Hide file tree
Showing 22 changed files with 282 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 05/04_deadlock/07/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
29 changes: 29 additions & 0 deletions 05/04_deadlock/07/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <string>
#include <thread>
#include <mutex>

int main() {
std::mutex mtx1;
std::mutex mtx2;

std::thread t1([&] {
for (int i = 0; i < 1000; i++) {
mtx1.lock();
mtx2.lock();
mtx2.unlock();
mtx1.unlock();
}
});
std::thread t2([&] {
for (int i = 0; i < 1000; i++) {
mtx2.lock();
mtx1.lock();
mtx1.unlock();
mtx2.unlock();
}
});
t1.join();
t2.join();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/07/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
10 changes: 10 additions & 0 deletions 05/04_deadlock/08/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
29 changes: 29 additions & 0 deletions 05/04_deadlock/08/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <string>
#include <thread>
#include <mutex>

int main() {
std::mutex mtx1;
std::mutex mtx2;

std::thread t1([&] {
for (int i = 0; i < 1000; i++) {
mtx1.lock();
mtx2.lock();
mtx2.unlock();
mtx1.unlock();
}
});
std::thread t2([&] {
for (int i = 0; i < 1000; i++) {
mtx1.lock();
mtx2.lock();
mtx2.unlock();
mtx1.unlock();
}
});
t1.join();
t2.join();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/08/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
10 changes: 10 additions & 0 deletions 05/04_deadlock/09/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
29 changes: 29 additions & 0 deletions 05/04_deadlock/09/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <string>
#include <thread>
#include <mutex>

int main() {
std::mutex mtx1;
std::mutex mtx2;

std::thread t1([&] {
for (int i = 0; i < 1000; i++) {
mtx1.lock();
mtx1.unlock();
mtx2.lock();
mtx2.unlock();
}
});
std::thread t2([&] {
for (int i = 0; i < 1000; i++) {
mtx2.lock();
mtx2.unlock();
mtx1.lock();
mtx1.unlock();
}
});
t1.join();
t2.join();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/09/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
10 changes: 10 additions & 0 deletions 05/04_deadlock/10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
21 changes: 21 additions & 0 deletions 05/04_deadlock/10/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <mutex>

std::mutex mtx1;

void other() {
mtx1.lock();
// do something
mtx1.unlock();
}

void func() {
mtx1.lock();
other();
mtx1.unlock();
}

int main() {
func();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/10/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
10 changes: 10 additions & 0 deletions 05/04_deadlock/11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
20 changes: 20 additions & 0 deletions 05/04_deadlock/11/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <mutex>

std::mutex mtx1;

/// NOTE: please lock mtx1 before calling other()
void other() {
// do something
}

void func() {
mtx1.lock();
other();
mtx1.unlock();
}

int main() {
func();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/11/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
10 changes: 10 additions & 0 deletions 05/04_deadlock/12/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
21 changes: 21 additions & 0 deletions 05/04_deadlock/12/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <mutex>

std::recursive_mutex mtx1;

void other() {
mtx1.lock();
// do something
mtx1.unlock();
}

void func() {
mtx1.lock();
other();
mtx1.unlock();
}

int main() {
func();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/12/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
10 changes: 10 additions & 0 deletions 05/04_deadlock/13/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
21 changes: 21 additions & 0 deletions 05/04_deadlock/13/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <mutex>

std::recursive_mutex mtx1;

void other() {
mtx1.lock();
// do something
mtx1.unlock();
}

void func() {
mtx1.lock();
other();
mtx1.unlock();
}

int main() {
func();
return 0;
}
6 changes: 6 additions & 0 deletions 05/04_deadlock/13/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

cmake -B build
cmake --build build
build/cpptest
Binary file modified 05/slides.pptx
Binary file not shown.

0 comments on commit 28af775

Please sign in to comment.