- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
2020 年 6 月 9 日更新:整合此处的所有评论和答案,并对此进行更多思考,我在下面创建了一个流程图(单击放大)来帮助决定何时使用 std::promise/future,以及需要权衡什么。
原帖如下:
我一直在思考 std::promise/future
机制的真正好处。几乎到处都在宣传这种模式的示例 - 单个生产者、单个生产者场景,其中生产者一次性通知消费者相关资源已准备好消费:
#include <iostream>
#include <future>
#include <thread>
using namespace std::chrono_literals;
struct StewableFood {
int tenderness;
};
void slow_cook_for_12_hours(std::promise<StewableFood>& promise_of_stew) {
std::cout << "\nChef: Starting to cook ...";
// Cook till 100% tender
StewableFood food{ 0 };
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(10ms);
food.tenderness = (i + 1) * 10;
std::cout << "\nChef: Stewing ... " << food.tenderness << "%";
}
// Notify person waiting on the promise of stew that the promise has been fulfilled.
promise_of_stew.set_value(food);
std::cout << "\nChef: Stew is ready!";
}
void wait_to_eat_stew(std::future<StewableFood>& potenial_fulfilment_of_stew) {
std::cout << "\nJoe: Waiting for stew ...";
auto food = potenial_fulfilment_of_stew.get();
std::cout << "\nJoe: I have been notified that stew is ready. Tenderness " << food.tenderness << "%! Eat!";
}
int main()
{
std::promise<StewableFood> promise_of_stew;
auto potenial_fulfilment_of_stew = promise_of_stew.get_future();
std::thread async_cook(slow_cook_for_12_hours, std::ref(promise_of_stew));
std::thread async_eat(wait_to_eat_stew, std::ref(potenial_fulfilment_of_stew));
async_cook.join();
async_eat.join();
return 0;
}
对我来说,所有这些异步性都没有任何意义,因为最终,消费者在 future::get
上的阻塞等待使得这种用法相当于具有顺序生产然后的单线程用法。消耗。我最初认为我上面的例子是人为的。但是,如果我们查看 std::promise/future
对的一次性使用约束(即,您不能重写原始 Promise,也不能重新读取来自最初的 future ),因此上面的示例成为唯一可行的用例,因为:
如果上面示例中的使用模式确实是唯一可行的用例,那么与仅执行以下操作相比,使用 std::promise
没有任何优势:
void cook_stew_then_eat() {
auto stew = slow_cook_for_12_hours();
// wait 12 hours
eat_stew(stew);
}
int main() {
std::thread t(cook_stew_then_eat);
t.join();
return 0;
}
现在看来,这个结论似乎很可疑。我非常确定 std::promise
有一个很好的用例,它不能被单线程顺序生产然后消费版本替换,该版本不涉及std::promise
。
问题:该用例是什么?
注意:很容易推测,也许std::promise/future
以某种方式允许我们异步执行其他操作而无需等待满足感——这可能是优势吗?绝对不是,因为我们可以通过将“其他东西”(例如一些重要的工作)放在另一个线程中来实现相同的效果。举例说明:
// cook and eat threads use std::promise/future
std::thread cook(...);
std::thread eat(...);
// Let's do important work on another thread
std::thread important_work(...);
cook.join();
eat.join();
important_work.join();
与不使用 std::promise/future
的解决方案相同:
// sequentially cook then eat, NO NEED to use std::promise/future
std::thread cook_then_eat(...);
// Let's do important work on another thread
std::thread important_work(...);
cook_then_eat.join();
important_work.join();
最佳答案
不,你实际上是正确的,如果你小心同步和对象生命周期, future / promise 模式总是可以被手动线程管理(通过线程连接、条件变量和互斥体)取代。
future / promise 模式的主要好处是抽象。它向您隐藏了共享状态的生命周期管理和同步,使您摆脱了自己执行此操作的负担。
一旦生产者有了 promise ,它就不需要了解有关消费方的任何其他信息,对于消费者和 future 也是如此。这使得编写更简洁、更不易出错且耦合更少的代码成为可能。
另请记住,从 C++20 开始,std::future
仍然缺少 continuations ,这使得它的功能远不如应有的强大。
关于c++ - std::promise 的任何情况都不能被运行顺序生产然后消费的单个线程替换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62258670/
我正在创建一个 sql server 存储过程,它将输入作为逗号分隔的 productid 或选项“全部”。当用户传入逗号分隔的产品 ID 时,查询应输出所有产品 ID 的数据。我使用“IN”语句执行
我有一个自动生成的 Web 服务客户端。我有很多复杂的类,我必须对其进行模式匹配。现在我的结构如下所示: val response = client.getResponse response matc
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 7年前关闭。 Improve this
我需要正确的 tsql 语法来解决这个问题: Select * from table where var_A='10' select * from table where var_B='10' 何时使
我遇到了这个问题。每当我运行程序并在需要时键入字母 m 时,我的 if 语句都不会识别它。有人知道为什么吗?我已经这样做了一个小时,但没有结果。 #include #include #includ
我从数据库列名称“你有护照”创建了一个表,用户回答是或否我如何将 css 应用到这个动态工作的表。 table, th, td { border: 1px solid black;
我对 LocationListener 类的 onStatusChanged 有一些疑问。 它知道它可以呈现三种状态:AVAILABLE、TEMPORARILY_UNAVAILABLE 和 OUT_O
当引入新的异常类型时,我总是不确定如何正确地做到这一点。有共同约定吗?你怎么做呢? 我对您组织它们的范围感兴趣(将它们保留在它们所使用的单元中?在组件级别有一个单元?包级别?应用程序?) 这也会影响命
我使用以下内容创建了日期维度: https://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-D
您好,我正在使用 Android 完全 Kiosk 浏览器,该浏览器使用 chrome Webview。但是 javascript 中的某些方法或函数无法正常工作,例如 window.print()。
我有以下代码: public void OpenFile(string FileName) { if (FileName == null)
获取索引越界异常 for (int recordData = 0; recordData < recordDataList.size(); recordData++) {
我使用它在发生错误时在登录中显示一条消息: × Invalid user or password
这是我的场景,我有一个异常列表,其中包含来自不同层次结构的任意异常,下面的代码快照将解释我需要做什么 private List connectionExceptions; try { // tryin
我尝试动态更新 Jtextpane 中的左缩进。但我不能!这是我尝试过的! DefaultStyledDocument document = (DefaultStyledDocument) textp
我不知道为什么这个异常不起作用...... import java.util.*; public class a { public static void main(String[] args
我目前在 case 中使用多个 when 时遇到问题。当我删除第二个当时,它就起作用了。这是什么问题? 报告的MYSQL错误为: #1064 - You have an error in your S
例如,我有一个表记录用户查看和下载文件的事件, file_id user activity 2 Tim view 1 Ron
这是一个非常愚蠢的问题,但我需要一点安慰/帮助。我有当前的“递归”情况: void add( int value ) { // do something ... // if ( conditi
我尝试使用以下代码在按钮数组上注册回调。但我似乎无法理解如何绑定(bind)回调中需要的字符串。任何建议将不胜感激! for (var i = 0; i < this.car_types.length
我是一名优秀的程序员,十分优秀!