元ネタは: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 にあります。