- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
获得 std::future
的一种方法是通过 std::async
:
int foo()
{
return 42;
}
...
std::future<int> x = std::async(foo);
在这个例子中,x
的异步状态的存储是如何分配的,哪个线程(如果涉及多个线程)负责执行分配?此外,std::async
的客户端是否可以控制分配?
对于上下文,我看到 one of the constructors std::promise
可能会收到一个分配器,但我不清楚是否可以在 级别自定义
.std::future
的分配>std::async
最佳答案
内存由调用std::async
的线程分配,你无法控制它是如何完成的。通常,它将由 new __internal_state_type
的某些变体完成, 但不能保证;它可能会使用 malloc
,或为此目的专门选择的分配器。
从 30.6.8p3 [futures.async] 开始:
"Effects: The first function behaves the same as a call to the second function with a policy argument of
launch::async | launch::deferred
and the same arguments forF
andArgs
. The second function creates a shared state that is associated with the returned future object. ..."
“第一个函数”是没有启动策略的过载,而第二个是有启动策略的过载。
在std::launch::deferred
的情况下,没有其他线程,所以一切都必须发生在调用线程上。在std::launch::async
的情况下, 30.6.8p3 继续说:
— if
policy & launch::async
is non-zero — callsINVOKE (DECAY_COPY (std::forward<F>(f)), DECAY_COPY (std::forward<Args>(args))...)
(20.8.2, 30.3.1.2) as if in a new thread of execution represented by a thread object with the calls toDECAY_COPY ()
being evaluated in the thread that calledasync
. ...
我添加了重点。由于函数和参数的复制必须发生在调用线程中,这实质上要求共享状态由调用线程分配。
当然,您可以编写启动新线程的实现,等待它分配状态,然后返回 future
。那引用了那个,但你为什么要这样做?
关于c++ - 与 std::future 关联的存储是如何分配的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834281/
我正在通过这个示例https://www.rusoto.org/futures.html学习Rust和Rusoto 而且我发现许多代码已经过时了。所以我改变了这样的代码: use rusoto_cor
这是一个理论问题。我有一个服务可以调用来完成工作,但该服务可能无法完成所有工作,因此我需要调用第二个服务来完成它。 我想知道是否有办法在没有 Await.result 的情况下做类似的事情map 函数
这个问题是关于如何阅读 Rust 文档并提高我对 Rust 的理解,从而了解如何解决这个特定的编译器错误。 我读过 tokio docs并试验了许多 examples .在编写自己的代码时,我经常遇到
我有一个使用分页的 HTTP api,我想将它包装到一个通用的 Rust 流中,以便所有端点都可以使用相同的接口(interface),这样我就可以使用 Stream 附带的特征函数特征。 我收到了这
我正在查看 AKKA 的 Java Futures API,我看到了很多处理同一类型的多个 future 的方法,但我没有看到任何处理不同类型的 future 的方法。我猜我让事情变得更加复杂了。 无
环境:Akka 2.1,scala 版本 2.10.M6,JDK 1.7,u5 现在是我的问题: 我有: future1 = Futures.future(new Callable>(){...});
我有一些代码可以将请求提交给另一个线程,该线程可能会也可能不会将该请求提交给另一个线程。这会产生 Future> 的返回类型.是否有一些非令人发指的方法可以立即将其变成 Future等待整个 futu
如果我有以下代码: Future a = new Future(() { print('a'); return 1; }); Future b = new Future.error('Error!')
我一直试图简化我在 Scala 中做 future 的方式。我有一次收到了 Future[Option[Future[Option[Boolean]]但我在下面进一步简化了它。有没有更好的方法来简化这
Scala 中从 Future[Option[Future[Int]]] 转换的最干净的方法是什么?至 Future[Option[Int]] ?甚至有可能吗? 最佳答案 有两个嵌套Future s
使用下面的示例,future2 如何在 future1 完成后使用 future1 的结果(不阻塞 future3 从被提交)? from concurrent.futures import Proc
这两个类代表了并发编程的优秀抽象,因此它们不支持相同的 API 有点令人不安。 具体根据docs : asyncio.Future is almost compatible with concurre
我正在尝试使用 wasm_bindgen 实现 API 类使用异步调用。 #![allow(non_snake_case)] use std::future::Future; use serde::{
这个问题在这里已经有了答案: Futures / Success race (3 个回答) 去年关闭。 所有的 future 最终可能会成功(有些可能会失败),但我们希望第一个成功。并希望将这一结果表
我在练习asyncio在编写多线程代码多年之后。 注意到一些我觉得很奇怪的东西。都在 asyncio在 concurrent有一个Future目的。 from asyncio import Futur
如何将Future[Option[Future[Option[X]]]]转换为Future[Option[X]]? 如果它是 TraversableOnce 而不是 Option 我会使用 Futur
我正在尝试同时发送 HTTP 请求。为此,我使用 concurrent.futures 这是简单的代码: import requests from concurrent import futures
我们在 vertx 中使用 Futures 的例子如下: Future fetchVehicle = getUserBookedVehicle(routingContext, client);
下面的函数,取自 here : fn connection_for( &self, pool_key: PoolKey, ) -> impl Future>, ClientError>
我正在围绕Java库编写一个小的Scala包装器。 Java库有一个对象QueryExecutor,它公开了2种方法: execute(query):结果 asyncExecute(query):Li
我是一名优秀的程序员,十分优秀!