stdasync

main thread waits for std::async to complete [duplicate]

ぃ、小莉子 提交于 2020-01-09 11:41:35
问题 This question already has answers here : Can I use std::async without waiting for the future limitation? (4 answers) Closed 2 years ago . I am using std::async to create a thread, I want this new thread should execute separately and main thread should not wait for it. But here when I call std::async, a new thread is created but main thread is waiting for completion of fun() . I want main thread to execute parallely without waiting for fun() to complete. How should I do that? #include

When to use std::async vs std::threads?

独自空忆成欢 提交于 2019-12-18 10:15:52
问题 Can anybody give a high level intuition about when to use each of them? References: Is it smart to replace boost::thread and boost::mutex with c++11 equivalents? When is it a good idea to use std::promise over the other std::thread mechanisms? 回答1: It's not really an either-or thing - you can use futures (together with promises) with manually created std::threads. Using std::async is a convenient way to fire off a thread for some asynchronous computation and marshal the result back via a

What is the difference between packaged_task and async

一个人想着一个人 提交于 2019-12-17 08:02:05
问题 While working with the threaded model of C++11, I noticed that std::packaged_task<int(int,int)> task([](int a, int b) { return a + b; }); auto f = task.get_future(); task(2,3); std::cout << f.get() << '\n'; and auto f = std::async(std::launch::async, [](int a, int b) { return a + b; }, 2, 3); std::cout << f.get() << '\n'; seem to do exactly the same thing. I understand that there could be a major difference if I ran std::async with std::launch::deferred , but is there one in this case? What

C++ async return value different when using x64

天大地大妈咪最大 提交于 2019-12-11 03:18:57
问题 The following Code compiles using both Debug\Win32 and Debug \ x64 settings in VC2013, but when I use Debug \ x64 i get the following IntelliSense Error : IntelliSense: no operator "=" matches these operands operand types are: std::future<bar<int>> = std::future<bar<int> (&)(bar<int> v)> The Error refers to this function in Header.cpp: bar<int> test::call(bar<int> v) { std::future<bar<int>> ret; ret = std::async(std::launch::async, &test::exec, this, v); return ret.get(); } Why does std:

What is the lifetime of the arguments of std::async?

感情迁移 提交于 2019-12-10 14:55:29
问题 It appears that arguments of a function executed via std::async share the lifetime of the future: #include <iostream> #include <future> #include <thread> struct S { S() { std::cout << "S() " << (uintptr_t)this << std::endl; } S(S&& s) { std::cout << "S(&&) " << (uintptr_t)this << std::endl; } S(const S& s) = delete; ~S() { std::cout << "~S() " << (uintptr_t)this << std::endl; } }; int main() { { std::cout << "enter scope" << std::endl; auto func = [](S&& s) { std::cout << "func " << (uintptr

std::future returned from std::async hangs while going out of scope

不羁岁月 提交于 2019-12-08 07:12:36
问题 I am using a combination of std::async and std::future from C++ 11 . I am using to enforce a time_out on a certain activity that I do in my code which might take time as I try connecting to server. Following is how the code is: #include <future> #include <chrono> std::size_t PotentiallyLongRunningActivity() { using namespace std::chrono_literals; std::this_thread::sleep_for(10000s); return 10; } bool DoActivity() { bool activity_done = false; auto my_future_result(std::async(std::launch:

What is causing data race in std::async here?

我是研究僧i 提交于 2019-12-08 06:17:22
问题 I recently created a pattern searching program in Conway's Game of Life, but It ran too slow to be practical. So I decided to parallelize it, but I failed; it caused segmentation fault, which is very likely due to data race. A brief explanation of the code: /* ... */ #include <list> #include <mutex> #include <future> #include <iostream> #include <forward_list> int main() { /* ... */ while (true) { /* ... */ std::forward_list</*pattern type*/> results; std::list<std::future<void>> results

std::async - Implementation dependent usage?

心不动则不痛 提交于 2019-12-05 15:02:25
问题 I've been thinking about std::async and how one should use it in future compiler implementation. However, right now I'm a bit stuck with something that feels like a design flaw. The std::async is pretty much implementation dependent, with probably two variants of launch::async , one which launches the task into a new thread and one that uses a thread-pool/task-scheduler. However, depending one which one of these variants that are used to implement std::async , the usage would vary greatly.

std::async - Implementation dependent usage?

本秂侑毒 提交于 2019-12-04 00:27:51
I've been thinking about std::async and how one should use it in future compiler implementation. However, right now I'm a bit stuck with something that feels like a design flaw. The std::async is pretty much implementation dependent, with probably two variants of launch::async , one which launches the task into a new thread and one that uses a thread-pool/task-scheduler. However, depending one which one of these variants that are used to implement std::async , the usage would vary greatly. For the "thread-pool" based variant you would be able to launch a lot of small tasks without worrying

Is the Visual C++ implementation of std::async using a thread pool legal

耗尽温柔 提交于 2019-12-03 10:43:10
问题 Visual C++ uses the Windows thread pool (Vista's CreateThreadpoolWork if available and QueueUserWorkItem if not) when calling std::async with std::launch::async . The number of threads in the pool is limited. If create several tasks that run for a long time without sleeping (including doing I/O), the upcoming tasks in the queue won't get a chance to work. The standard (I'm using N4140) says that using std::async with std::launch::async ... calls INVOKE(DECAY_COPY(std::forward<F>(f)), DECAY