-
Notifications
You must be signed in to change notification settings - Fork 46
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
8 changed files
with
222 additions
and
11 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
code/ModernCpp-ConcurrentProgramming-Tutorial/27创建异步任务获取返回值.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,13 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <future> // 引入 future 头文件 | ||
|
||
void f() { | ||
std::cout << std::this_thread::get_id() << '\n'; | ||
} | ||
|
||
int main() { | ||
auto t = std::async([] {}); | ||
std::future<void> future{ std::move(t) }; | ||
future.wait(); // Error! 抛出异常 | ||
} |
46 changes: 46 additions & 0 deletions
46
code/ModernCpp-ConcurrentProgramming-Tutorial/28future与 packaged_task.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,46 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <future> | ||
|
||
template<typename R, typename...Ts, typename...Args> | ||
void async_task(std::packaged_task<R(Ts...)>& task, Args&&...args) { | ||
// todo.. | ||
task(std::forward<Args>(args)...); | ||
} | ||
|
||
int main() { | ||
std::packaged_task<int(int, int)> task([](int a, int b) { | ||
return a + b; | ||
}); | ||
|
||
int value = 50; | ||
|
||
std::future<int> future = task.get_future(); | ||
|
||
// 创建一个线程来执行异步任务 | ||
std::thread t{ [&] { async_task(task, value, value); } }; | ||
std::cout << future.get() << '\n'; | ||
t.join(); | ||
} | ||
|
||
//int main(){ | ||
// std::cout << "main: " << std::this_thread::get_id() << '\n'; | ||
// | ||
// // 只能移动不能复制 | ||
// std::packaged_task<double(int, int)> task{ [](int a, int b) { | ||
// std::cout << "packaged_task: " << std::this_thread::get_id() << '\n'; | ||
// return std::pow(a, b); | ||
// } }; | ||
// | ||
// std::future<double> future = task.get_future(); | ||
// | ||
// // task(10, 2); // 调用 此处执行任务 | ||
// | ||
// std::thread t{ std::move(task) ,10,2 }; | ||
// | ||
// std::cout << "------\n"; | ||
// | ||
// std::cout << future.get() << '\n'; // 会阻塞,直到任务执行完毕 | ||
// | ||
// t.join(); | ||
//} |
55 changes: 55 additions & 0 deletions
55
code/ModernCpp-ConcurrentProgramming-Tutorial/29使用promise.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,55 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <future> | ||
#include <chrono> | ||
using namespace std::chrono_literals; | ||
|
||
void f(std::promise<int> obj ,int num){ | ||
// todo.. | ||
obj.set_value(num * num); // 调用了 set_value | ||
// todo.. | ||
std::this_thread::sleep_for(5s); // 模拟一些计算 | ||
} | ||
|
||
void throw_function(std::promise<int> prom) { | ||
prom.set_value(100); | ||
try { | ||
// todo.. | ||
throw std::runtime_error("一个异常"); | ||
} | ||
catch (...) { | ||
try { | ||
// 共享状态的 promise 已存储值,调用 set_exception 产生异常 | ||
prom.set_exception(std::current_exception()); | ||
} | ||
catch (std::exception& e) { | ||
std::cerr << "来自 set_exception 的异常: " << e.what() << '\n'; | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
std::promise<int> prom; | ||
std::future<int> fut = prom.get_future(); | ||
|
||
std::thread t(throw_function, std::move(prom)); | ||
|
||
std::cout << "等待线程执行,抛出异常并设置\n"; | ||
std::cout << "值:" << fut.get() << '\n'; // 100 | ||
|
||
t.join(); | ||
} | ||
|
||
|
||
//int main(){ | ||
// std::promise<int> promise; | ||
// | ||
// auto future = promise.get_future(); // 关联了 | ||
// | ||
// std::thread t{ f,std::move(promise), 10 }; | ||
// // f(std::move(promise), 10); | ||
// | ||
// std::cout << future.get() << '\n'; // 阻塞,直至结果可用 | ||
// std::cout << "end\n"; | ||
// t.join(); | ||
//} |
16 changes: 16 additions & 0 deletions
16
code/ModernCpp-ConcurrentProgramming-Tutorial/30future的状态变化.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,16 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <future> | ||
|
||
int main(){ | ||
std::future<void>future = std::async([] {}); | ||
std::cout << std::boolalpha << future.valid() << '\n'; // true | ||
future.get(); | ||
std::cout << std::boolalpha << future.valid() << '\n'; // false | ||
try { | ||
future.get(); // 抛出 future_errc::no_state 异常 | ||
} | ||
catch (std::exception& e) { | ||
std::cerr << e.what() << '\n'; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
code/ModernCpp-ConcurrentProgramming-Tutorial/31多个线程的等待shared_future.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,37 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <future> | ||
|
||
std::string fetch_data() { | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); // 模拟耗时操作 | ||
return "从网络获取的数据!"; | ||
} | ||
|
||
int main() { | ||
std::future<std::string> future_data = std::async(std::launch::async, fetch_data); | ||
|
||
// // 转移共享状态,原来的 future 被清空 valid() == false | ||
std::shared_future<std::string> shared_future_data = future_data.share(); | ||
|
||
// 多个线程持有一个 shared_future 对象并操作 | ||
|
||
// 第一个线程等待结果并访问数据 | ||
std::thread thread1([shared_future_data] { | ||
std::cout << "线程1:等待数据中..." << std::endl; | ||
shared_future_data.wait(); // 等待结果可用 | ||
std::cout << "线程1:收到数据:" << shared_future_data.get() << std::endl; | ||
}); | ||
|
||
// 第二个线程等待结果并访问数据 | ||
std::thread thread2([shared_future_data] { | ||
std::cout << "线程2:等待数据中..." << std::endl; | ||
shared_future_data.wait(); | ||
std::cout << "线程2:收到数据:" << shared_future_data.get() << std::endl; | ||
}); | ||
|
||
thread1.join(); | ||
thread2.join(); | ||
|
||
std::promise<std::string> p; | ||
std::shared_future<std::string> sf{ p.get_future() }; // 隐式转移所有权 | ||
} |
14 changes: 14 additions & 0 deletions
14
code/ModernCpp-ConcurrentProgramming-Tutorial/32限时等待-时钟.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,14 @@ | ||
#include <chrono> | ||
#include <iostream> | ||
#include <iomanip> | ||
using namespace std::chrono_literals; | ||
|
||
int main(){ | ||
auto now = std::chrono::system_clock::now(); | ||
time_t now_time = std::chrono::system_clock::to_time_t(now); | ||
std::cout << "Current time:\t" << std::put_time(std::localtime(&now_time), "%H:%M:%S\n"); | ||
|
||
auto now2 = std::chrono::steady_clock::now(); | ||
now_time = std::chrono::system_clock::to_time_t(now); | ||
std::cout << "Current time:\t" << std::put_time(std::localtime(&now_time), "%H:%M:%S\n"); | ||
} |
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