future

std::async variant which works over a collection

China☆狼群 提交于 2019-12-12 14:50:53
问题 Using std::async I was wondering whether it is possible to have a helper function, which creates std::future s from a collection (one future for every collection element). Often I have the following situation: auto func = []( decltype(collection)::reference value ) { //Some async work }; typedef std::result_of<decltype(func)>::type ResultType; std::vector<std::future<ResultType>> futures; futures.reserve(collection.size()); // Create all futures for( auto& element : collection ) { futures

How can I test a future that is bound to a tokio TcpStream?

我的梦境 提交于 2019-12-12 14:11:39
问题 I have a future which wraps a TCP stream in a Framed using the LinesCodec . When I try to wrap this in a test, I get the future blocking around 20% of the time, but because I have nothing listening on the socket I'm trying to connect to, I expect to always get the error: thread 'tokio-runtime-worker-0' panicked at 'error: Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }', src/lib.rs:35:24 note: Run with 'RUST_BACKTRACE=1' for a backtrace. This is the test code I have

Fork-Join related: join() vs get() vs invoke()

南楼画角 提交于 2019-12-12 11:34:19
问题 Is it necessary that I use join() with fork() or I may use also either of join() , get() , invoke() . I checked the API and besides that get() throws InterruptedException and ExecutionException I don't see differences... And invoke() seems totally the same. However I have always seen related fork() with join() rather than the other two methods... don't they provide parallelism? What's the purpose of having invoke() and join() totally the same? I can understand get() got by implementing future

Java Thread - weird Thread.interrupted() and future.cancel(true) behaviour

前提是你 提交于 2019-12-12 10:57:57
问题 I want to manage a list of Futures objects returned by my TaskExecutor. I've something like this List<Future<String>> list void process(ProcessThis processThis) { for ( ...) { Future<String> future = taskExecutor.submit(processThis); list.add(future) } } void removeFutures() { for(Future future : list) { assert future.cancel(true); } ProcessThis is a task that implements Callable< String> and checks for Thread.interrupted() status public String call() { while (true) { if (Thread.interrupted()

Storing a future in a list

荒凉一梦 提交于 2019-12-12 07:49:42
问题 I want to store the futures of several threads spawned using async in a list to retrieve their results later. future<int> f = async(doLater, parameter); list<future<int>> l; l.push_back(f); However the compiler prints the following error message /usr/include/c++/4.7/bits/stl_list.h:115:71: error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = int; std::future<_Res> = std::future]' Am i doing something wrong or aren't lists supposed to store futures?

What are futures?

风流意气都作罢 提交于 2019-12-12 07:47:00
问题 What are futures? It's something to do with lazy evaluation. 回答1: There is a Wikipedia article about futures. In short, it's a way to use a value that is not yet known. The value can then be calculated on demand (lazy evaluation) and, optionally, concurrently with the main calculation. C++ example follows. Say you want to calculate the sum of two numbers. You can either have the typical eager implementation: int add(int i, int j) { return i + j; } // first calculate both Nth_prime results

Computation with Futures avoiding Await method

☆樱花仙子☆ 提交于 2019-12-12 06:56:59
问题 I have funtion which is calling computeParallel() function which is calling 3 Futures F1,F2,F3 and returning String as return type. def computeParallel():String = { val f1 = Future { "ss" } val f2 = Future { "sss" } val f3 = Future { "ssss" } val result: Future[String] = for { r1 <- f1 r2 <- f2 r3 <- f3 } yield (r1 + r2 + r3) Await.result(result,scala.concurrent.duration.Duration.Inf) } Using Await to collect the Aggregated Results.But People are saying usage of Await is Bad way of coding. So

MSVC 2013 std::async works asynchronously without wait()

馋奶兔 提交于 2019-12-12 04:32:13
问题 Here is my code snippet to test if std::async indeed can be launched asynchronously on MSVC2013. And the result seems to verify that ( no pun intended), however the async() blocks on wait(). Now I am confused that so many queries on the forum discussed on the std::async synchronous (blocking) nature. With auto f = async(launch::async, [] {}); the code works still in async mode, but with f.get() it becomes sync. What am I doing wrong here? Does std::async have the creation overhead like the

Scala Pattern: For Comprehension that Yields a Future[A]

橙三吉。 提交于 2019-12-12 04:24:59
问题 What is the pattern used in Scala to deal with the scenario: You have a bunch of futures (they can be whatever, but for the sake of example...) val aF = Future { true } val bF = Future { Option(3) } val cF = Future { myObject } and you have some function that returns a future def fooF: Future[SomeObject] I want to do something like: for { a <- aF b <- bF c <- cF } yield { if (a) { // do stuff with b & c fooF } else { Future.successful(SomeObject) } } I want to return a value of Future

Sorting based on Future Result

社会主义新天地 提交于 2019-12-12 03:36:42
问题 I've trying to sort a list by a future boolean. I have a list of IDs and I need to query an external service to find out if there's contextual information behind them. The method I use to do this returns an optional future. By using the partition method I hoped to create two lists of IDs, one with contextual information and one without. The following answer on here provided a lot of help for this: Scala - sort based on Future result predicate I now have an rough, untested method that looks