7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++17(か!?): resumable / await で 非同期処理を待ち合わせる (コンソールアプリ)

Last updated at Posted at 2013-12-22

元ネタは:VisualStudio2013NovCTP:C++17(予定)resumable/await紹介からいただきました。

#include <iostream>
#include <thread>
#include <chrono>

// concurrency名前空間
#include <ppltasks.h>
// Nov2013CTP で新たに追加。resumable/await には必須
#include <pplawait.h> 

using namespace std;
using namespace concurrency;

task<void> consume_time(int millisec) __resumable {
    cout << "consume_time: " << millisec << "[ms] ほど眠らせていただきます...\n";
    chrono::milliseconds duration(millisec);
    std::this_thread::sleep_for(duration);
    cout << "consume_time: ...おはよーございますっ!\n";
}

task<void> async_proc(int millisec) __resumable {
    cout << "async_proc: 時間のかかるお仕事をさせ、終わるのを待ちます\n";
    __await consume_time(millisec);
    cout << "async_proc: 完了しました。\n";
}

int main() {
    cout << "main: はじめますよ\n";
    auto task = async_proc(1000);
    cout << "main: まだかなまだかなー\n";
    task.wait();
    cout << "main:: これでおしまい\n";
}

/* 実行結果:
main: はじめますよ
async_proc: 時間のかかるお仕事をさせ、終わるのを待ちます
consume_time: 1000[ms] ほど眠らせていただきます...
consume_time: ...おはよーございますっ!
main: まだかなまだかなー
async_proc: 完了しました。
main:: これでおしまい
*/

※ Visual C++ Compiler Compiler November 2013 CTP は http://aka.ms/Icp591 にあります。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?