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
21 changed files
with
204 additions
and
9 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
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
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
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,7 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
project(cpptest LANGUAGES CXX) | ||
|
||
add_executable(cpptest main.cpp) |
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,9 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <chrono> | ||
|
||
int main() { | ||
auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(400); | ||
std::this_thread::sleep_until(t); | ||
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
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
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
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
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,36 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <string> | ||
#include <vector> | ||
|
||
void download(std::string file) { | ||
for (int i = 0; i < 10; i++) { | ||
std::cout << "Downloading " << file | ||
<< " (" << i * 10 << "%)..." << std::endl; | ||
std::this_thread::sleep_for(std::chrono::milliseconds(400)); | ||
} | ||
std::cout << "Download complete: " << file << std::endl; | ||
} | ||
|
||
void interact() { | ||
std::string name; | ||
std::cin >> name; | ||
std::cout << "Hi, " << name << std::endl; | ||
} | ||
|
||
std::vector<std::thread> pool; | ||
|
||
void myfunc() { | ||
std::thread t1([&] { | ||
download("hello.zip"); | ||
}); | ||
// 移交控制权到全局的 pool 列表,以延长 t1 的生命周期 | ||
pool.push_back(std::move(t1)); | ||
} | ||
|
||
int main() { | ||
myfunc(); | ||
interact(); | ||
for (auto &t: pool) t.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,48 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <string> | ||
#include <vector> | ||
|
||
void download(std::string file) { | ||
for (int i = 0; i < 10; i++) { | ||
std::cout << "Downloading " << file | ||
<< " (" << i * 10 << "%)..." << std::endl; | ||
std::this_thread::sleep_for(std::chrono::milliseconds(400)); | ||
} | ||
std::cout << "Download complete: " << file << std::endl; | ||
} | ||
|
||
void interact() { | ||
std::string name; | ||
std::cin >> name; | ||
std::cout << "Hi, " << name << std::endl; | ||
} | ||
|
||
class ThreadPool { | ||
std::vector<std::thread> m_pool; | ||
|
||
public: | ||
void push_back(std::thread thr) { | ||
m_pool.push_back(std::move(thr)); | ||
} | ||
|
||
~ThreadPool() { // main 函数退出后会自动调用 | ||
for (auto &t: m_pool) t.join(); // 等待池里的线程全部执行完毕 | ||
} | ||
}; | ||
|
||
ThreadPool tpool; | ||
|
||
void myfunc() { | ||
std::thread t1([&] { | ||
download("hello.zip"); | ||
}); | ||
// 移交控制权到全局的 pool 列表,以延长 t1 的生命周期 | ||
tpool.push_back(std::move(t1)); | ||
} | ||
|
||
int main() { | ||
myfunc(); | ||
interact(); | ||
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 20) | ||
|
||
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,36 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <string> | ||
#include <vector> | ||
|
||
void download(std::string file) { | ||
for (int i = 0; i < 10; i++) { | ||
std::cout << "Downloading " << file | ||
<< " (" << i * 10 << "%)..." << std::endl; | ||
std::this_thread::sleep_for(std::chrono::milliseconds(400)); | ||
} | ||
std::cout << "Download complete: " << file << std::endl; | ||
} | ||
|
||
void interact() { | ||
std::string name; | ||
std::cin >> name; | ||
std::cout << "Hi, " << name << std::endl; | ||
} | ||
|
||
// ~jthread() 解构函数里会自动调用 join(),如果 joinable() 的话 | ||
std::vector<std::jthread> pool; | ||
|
||
void myfunc() { | ||
std::jthread t1([&] { | ||
download("hello.zip"); | ||
}); | ||
// 移交控制权到全局的 pool 列表,以延长 t1 的生命周期 | ||
pool.push_back(std::move(t1)); | ||
} | ||
|
||
int main() { | ||
myfunc(); | ||
interact(); | ||
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.