Skip to content

Commit

Permalink
add jthread
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jan 8, 2022
1 parent 66f8ded commit 2ee7dbd
Show file tree
Hide file tree
Showing 21 changed files with 204 additions and 9 deletions.
2 changes: 1 addition & 1 deletion 05/00_time/01/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::milliseconds>(dt).count();
int64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(dt).count();
std::cout << "time elapsed: " << ms << " ms" << std::endl;
return 0;
}
8 changes: 7 additions & 1 deletion 05/00_time/02/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include <chrono>

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, std::milli>;
double ms = std::chrono::duration_cast<double_ms>(dt).count();
std::cout << "time elapsed: " << ms << " ms" << std::endl;
return 0;
}
3 changes: 1 addition & 2 deletions 05/00_time/03/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <chrono>

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;
}
7 changes: 7 additions & 0 deletions 05/00_time/04/CMakeLists.txt
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)
9 changes: 9 additions & 0 deletions 05/00_time/04/main.cpp
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;
}
6 changes: 6 additions & 0 deletions 05/00_time/04/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
2 changes: 1 addition & 1 deletion 05/01_thread/02/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void interact() {

int main() {
std::thread t1([&] {
download("hello.zip");
download("hello.zip");
});
interact();
return 0;
Expand Down
2 changes: 1 addition & 1 deletion 05/01_thread/03/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion 05/01_thread/04/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void interact() {

void myfunc() {
std::thread t1([&] {
download("hello.zip");
download("hello.zip");
});
// 退出函数体时,会销毁 t1 线程的句柄!
}
Expand Down
2 changes: 1 addition & 1 deletion 05/01_thread/05/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void interact() {

void myfunc() {
std::thread t1([&] {
download("hello.zip");
download("hello.zip");
});
t1.detach();
// t1 所代表的线程被分离了,不再随 t1 对象销毁
Expand Down
10 changes: 10 additions & 0 deletions 05/01_thread/06/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)
36 changes: 36 additions & 0 deletions 05/01_thread/06/main.cpp
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;
}
6 changes: 6 additions & 0 deletions 05/01_thread/06/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/01_thread/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)
48 changes: 48 additions & 0 deletions 05/01_thread/07/main.cpp
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;
}
6 changes: 6 additions & 0 deletions 05/01_thread/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/01_thread/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 20)

project(cpptest LANGUAGES CXX)

add_executable(cpptest main.cpp)

find_package(Threads REQUIRED)
target_link_libraries(cpptest PUBLIC Threads::Threads)
36 changes: 36 additions & 0 deletions 05/01_thread/08/main.cpp
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;
}
6 changes: 6 additions & 0 deletions 05/01_thread/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
Binary file modified 05/slides.pptx
Binary file not shown.
2 changes: 1 addition & 1 deletion hw05
Submodule hw05 updated 3 files
+3 −0 CMakeLists.txt
+9 −9 README.md
+85 −2 main.cpp

0 comments on commit 2ee7dbd

Please sign in to comment.