diff --git a/05/00_time/01/main.cpp b/05/00_time/01/main.cpp index 42189d54..2e010d96 100644 --- a/05/00_time/01/main.cpp +++ b/05/00_time/01/main.cpp @@ -7,7 +7,7 @@ int main() { for (volatile int i = 0; i < 10000000; i++); auto t1 = std::chrono::steady_clock::now(); auto dt = t1 - t0; - auto ms = std::chrono::duration_cast(dt).count(); + int64_t ms = std::chrono::duration_cast(dt).count(); std::cout << "time elapsed: " << ms << " ms" << std::endl; return 0; } diff --git a/05/00_time/02/main.cpp b/05/00_time/02/main.cpp index 3c88fb80..78555593 100644 --- a/05/00_time/02/main.cpp +++ b/05/00_time/02/main.cpp @@ -3,6 +3,12 @@ #include int main() { - std::this_thread::sleep_for(std::chrono::milliseconds(400)); + auto t0 = std::chrono::steady_clock::now(); + for (volatile int i = 0; i < 10000000; i++); + auto t1 = std::chrono::steady_clock::now(); + auto dt = t1 - t0; + using double_ms = std::chrono::duration; + double ms = std::chrono::duration_cast(dt).count(); + std::cout << "time elapsed: " << ms << " ms" << std::endl; return 0; } diff --git a/05/00_time/03/main.cpp b/05/00_time/03/main.cpp index 292b86f2..3c88fb80 100644 --- a/05/00_time/03/main.cpp +++ b/05/00_time/03/main.cpp @@ -3,7 +3,6 @@ #include int main() { - auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(400); - std::this_thread::sleep_until(t); + std::this_thread::sleep_for(std::chrono::milliseconds(400)); return 0; } diff --git a/05/00_time/04/CMakeLists.txt b/05/00_time/04/CMakeLists.txt new file mode 100644 index 00000000..1320e598 --- /dev/null +++ b/05/00_time/04/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.10) + +set(CMAKE_CXX_STANDARD 17) + +project(cpptest LANGUAGES CXX) + +add_executable(cpptest main.cpp) diff --git a/05/00_time/04/main.cpp b/05/00_time/04/main.cpp new file mode 100644 index 00000000..292b86f2 --- /dev/null +++ b/05/00_time/04/main.cpp @@ -0,0 +1,9 @@ +#include +#include +#include + +int main() { + auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(400); + std::this_thread::sleep_until(t); + return 0; +} diff --git a/05/00_time/04/run.sh b/05/00_time/04/run.sh new file mode 100755 index 00000000..bae2e1d6 --- /dev/null +++ b/05/00_time/04/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +cmake -B build +cmake --build build +build/cpptest diff --git a/05/01_thread/02/main.cpp b/05/01_thread/02/main.cpp index 2ed71e45..763388eb 100644 --- a/05/01_thread/02/main.cpp +++ b/05/01_thread/02/main.cpp @@ -19,7 +19,7 @@ void interact() { int main() { std::thread t1([&] { - download("hello.zip"); + download("hello.zip"); }); interact(); return 0; diff --git a/05/01_thread/03/main.cpp b/05/01_thread/03/main.cpp index b941dc61..e02cae39 100644 --- a/05/01_thread/03/main.cpp +++ b/05/01_thread/03/main.cpp @@ -19,7 +19,7 @@ void interact() { int main() { std::thread t1([&] { - download("hello.zip"); + download("hello.zip"); }); interact(); std::cout << "Waiting for child thread..." << std::endl; diff --git a/05/01_thread/04/main.cpp b/05/01_thread/04/main.cpp index 86e9e06f..e1acc982 100644 --- a/05/01_thread/04/main.cpp +++ b/05/01_thread/04/main.cpp @@ -19,7 +19,7 @@ void interact() { void myfunc() { std::thread t1([&] { - download("hello.zip"); + download("hello.zip"); }); // 退出函数体时,会销毁 t1 线程的句柄! } diff --git a/05/01_thread/05/main.cpp b/05/01_thread/05/main.cpp index fb876b93..f26c7dcd 100644 --- a/05/01_thread/05/main.cpp +++ b/05/01_thread/05/main.cpp @@ -19,7 +19,7 @@ void interact() { void myfunc() { std::thread t1([&] { - download("hello.zip"); + download("hello.zip"); }); t1.detach(); // t1 所代表的线程被分离了,不再随 t1 对象销毁 diff --git a/05/01_thread/06/CMakeLists.txt b/05/01_thread/06/CMakeLists.txt new file mode 100644 index 00000000..4800192c --- /dev/null +++ b/05/01_thread/06/CMakeLists.txt @@ -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) diff --git a/05/01_thread/06/main.cpp b/05/01_thread/06/main.cpp new file mode 100644 index 00000000..78c633ed --- /dev/null +++ b/05/01_thread/06/main.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +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 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; +} diff --git a/05/01_thread/06/run.sh b/05/01_thread/06/run.sh new file mode 100755 index 00000000..bae2e1d6 --- /dev/null +++ b/05/01_thread/06/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +cmake -B build +cmake --build build +build/cpptest diff --git a/05/01_thread/07/CMakeLists.txt b/05/01_thread/07/CMakeLists.txt new file mode 100644 index 00000000..4800192c --- /dev/null +++ b/05/01_thread/07/CMakeLists.txt @@ -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) diff --git a/05/01_thread/07/main.cpp b/05/01_thread/07/main.cpp new file mode 100644 index 00000000..8ae3b588 --- /dev/null +++ b/05/01_thread/07/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +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 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; +} diff --git a/05/01_thread/07/run.sh b/05/01_thread/07/run.sh new file mode 100755 index 00000000..bae2e1d6 --- /dev/null +++ b/05/01_thread/07/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +cmake -B build +cmake --build build +build/cpptest diff --git a/05/01_thread/08/CMakeLists.txt b/05/01_thread/08/CMakeLists.txt new file mode 100644 index 00000000..51e7a695 --- /dev/null +++ b/05/01_thread/08/CMakeLists.txt @@ -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) diff --git a/05/01_thread/08/main.cpp b/05/01_thread/08/main.cpp new file mode 100644 index 00000000..9ae85022 --- /dev/null +++ b/05/01_thread/08/main.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +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 pool; + +void myfunc() { + std::jthread t1([&] { + download("hello.zip"); + }); + // 移交控制权到全局的 pool 列表,以延长 t1 的生命周期 + pool.push_back(std::move(t1)); +} + +int main() { + myfunc(); + interact(); + return 0; +} diff --git a/05/01_thread/08/run.sh b/05/01_thread/08/run.sh new file mode 100755 index 00000000..bae2e1d6 --- /dev/null +++ b/05/01_thread/08/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e + +cmake -B build +cmake --build build +build/cpptest diff --git a/05/slides.pptx b/05/slides.pptx index 94df847e..66bb5322 100644 Binary files a/05/slides.pptx and b/05/slides.pptx differ diff --git a/hw05 b/hw05 index e9466688..d79a47db 160000 --- a/hw05 +++ b/hw05 @@ -1 +1 @@ -Subproject commit e9466688c45d698a8835e475fc3f09aab0c53107 +Subproject commit d79a47db143e81763a94d8da4a5a27036b5b2f68