stdasync

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

笑着哭i 提交于 2019-12-03 01:12:46
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_COPY(std::forward<Args>(args))...) (20.9.2, 30.3.1.2) as if in a new thread of execution represented by a

c++11 async continuations or attempt at .then() semantics

 ̄綄美尐妖づ 提交于 2019-11-28 17:18:54
The code below is based on Herb Sutter's ideas of an implementation of a .then() type continuation. template<typename Fut, typename Work> auto then(Fut f, Work w)->std::future<decltype(w(f.get()))> { return std::async([=] { w(f.get()); }); } This would be used like auto next = then(f, [](int r) { go_and_use(r); }); or similar. This is a neat idea, but as it stands will not work (futures are move only and not copyable). I do like the idea as it is likely to appear in upcoming versions of c++ as far as I can guess (although as .then() or even await.) Before making the futures shared or similar I

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

亡梦爱人 提交于 2019-11-28 13:59:08
This question already has an answer here: Can I use std::async without waiting for the future limitation? 4 answers 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 <iostream> #include <windows.h> #include <future> using namespace std; void printid() { cout << "Thread id is:" << this_thread::get

c++11 async continuations or attempt at .then() semantics

≯℡__Kan透↙ 提交于 2019-11-27 20:11:41
问题 The code below is based on Herb Sutter's ideas of an implementation of a .then() type continuation. template<typename Fut, typename Work> auto then(Fut f, Work w)->std::future<decltype(w(f.get()))> { return std::async([=] { w(f.get()); }); } This would be used like auto next = then(f, [](int r) { go_and_use(r); }); or similar. This is a neat idea, but as it stands will not work (futures are move only and not copyable). I do like the idea as it is likely to appear in upcoming versions of c++

Why should I use std::async?

大憨熊 提交于 2019-11-27 17:16:57
I'm trying to explore all the options of the new C++11 standard in depth, while using std::async and reading its definition, I noticed 2 things, at least under linux with gcc 4.8.1 : it's called async , but it got a really "sequential behaviour", basically in the row where you call the future associated with your async function foo , the program blocks until the execution of foo it's completed. it depends on the exact same external library as others, and better, non-blocking solutions, which means pthread , if you want to use std::async you need pthread. at this point it's natural for me

c++11 std::async doesn't work in mingw

↘锁芯ラ 提交于 2019-11-27 15:06:57
Running this code from Herb Sutter's presentation . This works fine in linux under gcc 4.6.3. I'm thinking that future.h isn't supported in mingw, but the error is really hard to understand! #include <iostream> #include <vector> #include <string> #include <future> #include <algorithm> using namespace std; string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector<future<string>> v; v.push_back( async([]{ return flip(" ,olleH"); }) ); v.push_back( async([]{ return flip(".gnaL"); }) ); v.push_back( async([]{ return flip("\n!TXEN"); }) ); for( auto& e: v ) { cout << e

What is the difference between packaged_task and async

懵懂的女人 提交于 2019-11-27 05:52:54
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 is the difference between these two approaches, and more importantly, in what use cases should I use one

Why should I use std::async?

情到浓时终转凉″ 提交于 2019-11-27 04:12:28
问题 I'm trying to explore all the options of the new C++11 standard in depth, while using std::async and reading its definition, I noticed 2 things, at least under linux with gcc 4.8.1 : it's called async , but it got a really "sequential behaviour", basically in the row where you call the future associated with your async function foo , the program blocks until the execution of foo it's completed. it depends on the exact same external library as others, and better, non-blocking solutions, which

Which std::async implementations use thread pools?

牧云@^-^@ 提交于 2019-11-26 22:03:58
One of the advantages of using std::async instead of manually creating std::thread objects is supposed to be that std::async can use thread pools under the covers to avoid oversubscription problems. But which implementations do this? My understanding is that Microsoft's implementation does, but what about these other async implementations? Gnu's libstdc++ Gnu's libc++ Just Software's library Boost (for boost::thread::async , not std::async ) Thanks for any information you can offer. Black-Box Test Though "white box" check can be done via inspecting boost, libstdc++ or libc++ sources, or

c++11 std::async doesn't work in mingw

戏子无情 提交于 2019-11-26 17:01:12
问题 Running this code from Herb Sutter's presentation. This works fine in linux under gcc 4.6.3. I'm thinking that future.h isn't supported in mingw, but the error is really hard to understand! #include <iostream> #include <vector> #include <string> #include <future> #include <algorithm> using namespace std; string flip( string s ) { reverse( begin(s), end(s) ); return s; } int main() { vector<future<string>> v; v.push_back( async([]{ return flip(" ,olleH"); }) ); v.push_back( async([]{ return