forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.