{"id":1459,"date":"2014-11-15T20:28:16","date_gmt":"2014-11-15T19:28:16","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=1459"},"modified":"2014-12-14T12:00:48","modified_gmt":"2014-12-14T11:00:48","slug":"visual-c-2015-resumable-functions","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2014\/11\/15\/visual-c-2015-resumable-functions\/","title":{"rendered":"Visual C++ 2015 &#8211; Resumable Functions"},"content":{"rendered":"<p>Visual C++ 2015 includes a general purpose solution to implement resumable functions based on the concept of <strong>coroutines<\/strong>. A coroutine is a generalized routine entity which supports operations like suspend and resume in addition to the traditional invoke and return operations.<br \/>\nThese resumable functions are being proposed for inclusion in ISO C++17.<br \/>\nFor the VC++ 2015 Preview, the feature only works for 64-bit targets, and requires adding the \/await switch to your compiler command-line.<br \/>\nSuch resumable functions have several use cases:<\/p>\n<ul>\n<li>Asynchronous operations<\/li>\n<li>Generator pattern<\/li>\n<li>Reactive Streams<\/li>\n<\/ul>\n<p>Here is a simple example demonstrating an asynchronous operation:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;future&gt;\r\n#include &lt;thread&gt;\r\n#include &lt;experimental\\resumable&gt;\r\n\r\nusing namespace std; \r\nusing namespace std::chrono; \r\n  \r\n\/\/ this could be some long running computation or I\/O\r\nfuture&lt;int&gt; calculate_the_answer() \r\n{ \r\n    return async(&#x5B;] { \r\n        this_thread::sleep_for(1s); return 42; \r\n    }); \r\n} \r\n  \r\n\/\/ Here is a resumable function\r\nfuture&lt;void&gt; coro() { \r\n    printf(&quot;Started waiting... \\n&quot;); \r\n    auto result = __await calculate_the_answer(); \r\n    printf(&quot;got %d. \\n&quot;, result); \r\n} \r\n  \r\nint _tmain(int argc, _TCHAR* argv&#x5B;]) \r\n{ \r\n    coro().get(); \r\n}\r\n<\/pre>\n<p>The important line here is line 17. The function calculate_the_answer() is an asynchronous function which immediately returns by returning a future<int>. Thanks to the __await keyword on line 17, the rest of the coro() function can be implemented <strong>as if<\/strong> you are simply programming synchronously! No need anymore to mess around with task continuations or what not. This makes asynchronous programming much easier \ud83d\ude00<\/p>\n<p><a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2014\/11\/12\/resumable-functions-in-c.aspx\" target=\"_blank\">Read the full explanation here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual C++ 2015 includes a general purpose solution to implement resumable functions based on the concept of coroutines. A coroutine is a generalized routine entity which supports operations like suspend and resume in addition to the traditional invoke and return operations. These resumable functions are being proposed for inclusion in ISO C++17. For the VC++ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,16],"tags":[210,209,208],"class_list":["post-1459","post","type-post","status-publish","format-standard","hentry","category-c","category-microsoft","tag-await","tag-resumable","tag-visual-c-2015"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1459"}],"version-history":[{"count":5,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1459\/revisions"}],"predecessor-version":[{"id":1473,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1459\/revisions\/1473"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}