简体   繁体   English

如何在不设置值的情况下通知 C++ 未来?

[英]How do I notify a c++ future without setting a value?

I have a thread waiting on a future.我有一个线程在等待未来。 In the event where my application is shut down, I would like to join the thread.如果我的应用程序关闭,我想加入该线程。 How do I go about notifying the future without fudging it and setting it to some value?我如何在不捏造未来并将其设置为某个值的情况下通知未来?

auto promise = std::promise<std::string>();
...
auto future = promise.get_future();
auto consumer = std::thread([&]
{
   while (future.wait_for(10ms)){
       // do something
       std::cout << future.get().c_str();
   }
});
...
consumer.join();

So std threading primitives are primitives.所以标准线程原语是原语。 They are ok for direct use in toy applications, where "shut down early" means "tell the OS to shut down the process".它们可以直接用于玩具应用程序,其中“提前关闭”意味着“告诉操作系统关闭进程”。

For more complex applications, you need to build things up from them.对于更复杂的应用程序,您需要根据它们进行构建。

You'll want thread pools instead of using raw threads, you'll probably want continuations of thread tasks, and you'll want a shutdown architecture.您需要线程池而不是使用原始线程,您可能需要线程任务的延续,并且您需要关闭架构。

One way to implement a shutdown architecture is to use exceptions.实现关闭架构的一种方法是使用异常。 If all of your outstanding future's have promises in a central bit of code, you can set exception on them.如果您所有未完成的未来都在一个核心代码中得到了承诺,您可以对它们 设置异常 Or destroy the promises.或者破坏承诺。 Both result in people waiting on the futures to get an exception thrown.两者都会导致人们等待期货以引发异常。

Or you can enforce that all of your promises/futures are actually future<optional<T>> and use "nullopt" to say "we are shutting down".或者你可以强制你所有的承诺/未来实际上都是future<optional<T>>并使用“nullopt”来表示“我们正在关闭”。 Or use future<expected<T,Error>> .或者使用future<expected<T,Error>>

On the other hand, if your async code is based on continuations (including "continue into a task queue for idle processing on main thread" and "continue into another async task"), you can have a policy for when the source of a continuation has failed.另一方面,如果您的异步代码基于延续(包括“继续进入任务队列以在主线程上进行空闲处理”和“继续进入另一个异步任务”),则您可以有一个策略,用于何时将延续源失败了。 Some continuation systems give the next one something like an std::experimental::expected<Data, Error> .一些延续系统给下一个类似std::experimental::expected<Data, Error> Others hand it a ready-or-error-state std::future<Data> .其他人则将其置于就绪或错误状态std::future<Data> In other cases, abandoned inputs of continutations cause the continuation to become abandoned automatically.在其他情况下,continuation 的放弃输入会导致 continuation 自动放弃。

Shutting down a C++ application is hard, especially if you want to do it on-demand.关闭 C++ 应用程序很困难,尤其是如果您想按需关闭。 It basically requires a global event that rolls-down all control paths in the app in a clean way.它基本上需要一个全局事件,以干净的方式滚动应用程序中的所有控制路径。 There isn't an easy answer.没有一个简单的答案。 There are many easy ways you can get it to sort of work in a way that doesn't seem to cause any immediate problems, like "just detach the thread" or "just call exit ".有许多简单的方法可以让它以一种似乎不会立即导致任何问题的方式工作,例如“只是分离线程”或“只是调用exit ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM