Microsoft Consumer Channels and Central Marketing Group 3/15/2017 C++/CXの非同期処理まとめ ~VC++とC++/CXについて~ 非同期勉強会 2014/5/10 Sat Room metro大阪 遥佐保 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
はじめに
自己紹介 @hr_sao Microsoft MVP for Client Development 出没コミュニティ Room metro 3/15/2017 自己紹介 @hr_sao Microsoft MVP for Client Development 出没コミュニティ Room metro →次回は5/20(火) C++テンプレート完全ガイド読書会 →次回は5/12(月) 次回は、5/12(月)にcpptmp読書会やるよ! 5/20(火)はめとべやエキスパート塾Uniity編やるよ © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VisualC++ 特に C++/CX関連の 本日の目的 VisualC++ 特に C++/CX関連の 非同期処理関連について 理解度を深める
Topics C++規格 非同期処理の活用 Visual Studio 2013 VC++ Visual C++ の非同期処理 C++/CX非同期処理 まとめ
1. C++の規格
Recently Published: C++11 (2011) 3/15/2017 Recently Published: C++11 (2011) http://isocpp.org/std/status C++冬の時代を乗り越え 今まさに空前絶後の C++バージョンアップブーム © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++2013事情 C++11/C++14/C++17(予定) 3/15/2017 VC++2013事情 C++11/C++14/C++17(予定) C++11 Rvalue references (対応完了) ref-qualifiers constexpr (一部) Alignment Inheriting constructors Defaulted and deleted functions (対応完了) Extended sizeof noexcept (一部) C++11 Concurrency Magic statics C++11 C99 __func__ (対応完了) C#のasync/awaitを意識 C#のasync/awaitを意識 C++14 auto and decltype(auto) return types Generic lambdas (一部) C++17 (予定) Concurrency TS(?) Resumable functions and await (一部) © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
2. 非同期処理の活用
非同期処理の活用例 1つプロセス内の処理 複数プロセス間の処理 他リソース間の処理 非同期処理 レスポンスタイムの向上 3/15/2017 非同期処理の活用例 1つプロセス内の処理 複数プロセス間の処理 他リソース間の処理 非同期処理 レスポンスタイムの向上 →「待たない」 並行処理 マルチスレッドの効率的利用 →並列処理 スループット向上 ex)ディスクアクセスの間に、CPUを利用する ex)DBからデータを取得するなど、「非同期」でよく使われる例はこれ © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
非同期処理の活用例 1 2 1つプロセス内の処理 複数プロセス間の処理 他リソース間の処理 非同期処理 レスポンスタイムの向上 3/15/2017 非同期処理の活用例 本日は、用語をこう定義して進めます 1 → 非同期処理 2 → 並列高速演算 1つプロセス内の処理 複数プロセス間の処理 他リソース間の処理 非同期処理 レスポンスタイムの向上 →「待たない」 並行処理 マルチスレッドの効率的利用 →並列処理 スループット向上 ex)ディスクアクセスの間に、CPUを利用する ex)DBからデータを取得するなど、「非同期」でよく使われる例はこれ 1 2 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
3/15/2017 3. Visual Studio 2013 VC++ © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
マイクロソフト社が提供するC++ライブラリ concurrency 名前空間 3/15/2017 非同期処理 C++11 std::future / std::promise / std::packaged_task / std::thread / std::async など →かなりコードが生々しい PPL Task マイクロソフト社が提供するC++ライブラリ concurrency 名前空間 いままではBoostライブラリなどで、各OSのスレッド処理を共通化したりしていた © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
PPL Task using concurrency → PPL : Parallel Patterns Library(MS提供) 3/15/2017 PPL Task using concurrency → PPL : Parallel Patterns Library(MS提供) VC++で用意されている非同期処理含めた 並列処理は concurrency 名前空間にまとめられて いる タスクの並列処理 (そのためにスレッドも利用) 並列アルゴリズム paralle_for_each 並列に行列演算させたり © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++2013 1 2 プロジェクト例 種類 機能の例 非同期処理 C++ C++11 <future> 3/15/2017 VC++2013 プロジェクト例 種類 機能の例 非同期処理 C++ C++11 <future> std::thread / std::promise std::async VC++ Windows Runtime C++/CX ppltasks.h concurrency::task 並列 高速演算 DirectX (GPGPU利用) C++AMP amp.h concurrency::parallel_for_each 1 Concurrencyの中には、広くVC++で使えるものと、Windowsストアアプリだけで使えるものとある 2 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++余談 Lambda grammer
Classic C #include <stdio.h> int main() { printf( “Hello World\n” ); return 0; }
C++03 #include <iostream> int main() { std::cout << “Hello World.” << std::endl; return 0; }
C++11 lambdas 最小定義 [ ]{ }; [ ]( ){ }; int main() { 3/15/2017 C++11 lambdas 最小定義 [ ]{ }; [ ]( ){ }; int main() { [ ]( ){ }( ); // 空のラムダ式 return 0; } © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
3/15/2017 C++11 (1) int main() { [ ]{ std::cout << “Hello World!” << std::endl; }( ); return 0; } {}の中が関数 ()で関数呼び出し © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++11 (2) int main() { auto f = [ ]{ 3/15/2017 C++11 (2) int main() { auto f = [ ]{ std::cout << “Hello World!” << std::endl; }; f( ); // 代入されたラムダを関数呼び出し return 0; } ラムダはオブジェクト © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++11 (3) int main() { [ ]( std::string const &str ) // 引数 3/15/2017 C++11 (3) int main() { [ ]( std::string const &str ) // 引数 { std::cout << str << std::endl; } // コード ( “Hello World!” ); // 呼び出し return 0; } Hello World文字を引数にしている © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++14 generic lambda int main() { auto plus = [ ]( auto x, auto y ) { 3/15/2017 C++14 generic lambda int main() { auto plus = [ ]( auto x, auto y ) { return x + y; }; plus( 5, 5 ); plus( std::string( "Hello “ ), std::string( "World“ )); return 0; } これを踏まえたうえで次 ちなみに以下generic lamdbaの例 <C++14> auto glambda = [ ] ( auto a ) { return a; }; <C++11> struct unnamed_lambda { template<typename T> auto operator( )(T a) const { return a; } auto lambda = unnamed_lambda(); © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
3/15/2017 4. Visual C++非同期処理 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++非同期処理(1) まずは ppltasks.h の concurrency::task を使ってみる
concurrency::task sample 3/15/2017 concurrency::task sample #include "stdafx.h“ #include <iostream> #include <ppltasks.h> // <future>でもOK int main( ){ int num = 0; concurency::task<int>t01([ &num ](){ // taskで別タスク実行 ++ num; return( num ); // 非同期処理で返すものを設定 }); int result = t01.get(); // 同期を取る return( 0 ); } std::asyncと同じ形 結局、concurrency::taskを使っても、この程度だと std::async と全く同じ © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
.then()を用いたTask のつなげ方 (1) concurency::task<int> t1([]() { return( 1 ); }); concurency::task<int> t2 = t1.then([](int n) { return( ++n ); }); concurency::task<int> t3 = t2.then([](int n) { return( ++n ); }); int result = t3.get(); // 同期 型は合わせること .then で返り値を 別のタスクに渡す どんどんつなげることが可能
.then()を用いたTask のつなげ方 (2) 3/15/2017 .then()を用いたTask のつなげ方 (2) concurency::task<int> t([](){ return( 1 ); }).then([](int n){ return( ++n ); }) この例だと、途中で例外が起きたら、あとの .then は実行されない Task<T>にしないといけない そういえばC#でも 似たような書き方 出来ましたね © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++非同期処理(2) pplawait.h の resumable / await を使ってみる
Visual C++ Compiler November 2013 CTP 3/15/2017 VC++非同期処理 resumable / await C#の async/await の C++版 Visual C++ Compiler November 2013 CTP http://aka.ms/Icp591 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++コンパイラを選択
resumable/await Sample 3/15/2017 resumable/await Sample #include <future> #include <pplawait.h> concurrency::task<void> my_proc(void) __resumable{ auto x = []() __resumable -> concurrency::task<void> { std::cout << “abc." << std::endl; }; __await x(); std::cout << “def." << std::endl; } int main() { auto task = my_proc(); task.wait(); __await の後ろはスタックコールバックと思ったらいい .then を書く必要がない © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
3/15/2017 5. C++/CX非同期処理 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++/CX事情 C++11 前提のコーディング 自作スレッドは使わない(PPL Task標準利用) 3/15/2017 C++/CX事情 C++11 前提のコーディング 自作スレッドは使わない(PPL Task標準利用) std::shared_ptr, ラムダ式など積極的に利用 C++の規格のバージョンアップに伴ってコー ディング方法もここ数年変化 これらの合わせ技 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++/CX Sample .then版 void App1::MainPage::my_btn_click( 3/15/2017 C++/CX Sample .then版 void App1::MainPage::my_btn_click( Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { task<StorageFile^>( KnownFolders::DocumentsLibrary->CreateFileAsync( my_txt->Text ,CreationCollisionOption::ReplaceExisting) ).then([this](StorageFile^ file) my_btn_01->Content = "ファイル作成しました"; }); } Windowsストアアプリでファイル操作を逆に同期にすることは出来ない(難しい) © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++/CX Sample resumable/await版 3/15/2017 C++/CX Sample resumable/await版 concurrency::task<void> App1::MainPage::my_btn_click( Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)__resumable { auto file = __await file->CreateFileAsync( my_txt->Text, CreationCollisionOption::ReplaceExisting); my_btn_01->Content = "ファイル作成しました"; } .thenよりもちょっとは見やすくなった…? © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++からC++/CXそして WindowsRuntimeでの活用 App container ( package manifest ) UI controls ( XAML ) 相互互換のための C++言語拡張機能 (中間の変換無し) C++実装Class (ネイティブ) VSプロジェクト テンプレート C++/CX
非同期構成/操作概念 UI controls C++/CX ( XAML ) 3/15/2017 非同期構成/操作概念 App container ( package manifest ) UI controls ( XAML ) C++/CX C++実装クラス (ネイティブ) concurrency::create_async Windows::Foundation::IAsyncActionなど concurrency::task 他のWindows Runtimeと 非同期通信 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
VC++のPPLで用意している 並列化の手法(C++ネイティブ) 3/15/2017 VC++のPPLで用意している 並列化の手法(C++ネイティブ) 処理 利用クラス/アルゴリズム/メソッド 非同期タスク処理 concurrency::task 並列に分割した処理の グルーピング concurrency::task_group concurrency::structured_task_group タスク処理の継続 concurrency::task::then concurrency::task はテンプレートクラス © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
WindowsRuntimeで用意しているTask 3/15/2017 WindowsRuntimeで用意しているTask 4つの非同期インターフェース Windows::Foundation 他の言語ともやり取りできる 非同期アクション IAsyncAction 非同期進行状況 IAsyncActionWithProgress<TProgress> 結果を返す非同期 IAsyncOperation<TResult> 結果も返すし進行も判る IAsyncOperationWithProgress<TResult, TProgress> やっとC#にも通じる話が出てきた © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
C++/CXで用意している 他言語互換用のTask 処理 利用クラス 非同期タスク処理 concurrency::create_async 他言語やり取りする場合は、create_async →4つの非同期インタフェースのどれかでラップ C++クラスの世界で完結する場合は taskでOK
(余談)C++/CXで用意している Windows Runtimeのスレッド 3/15/2017 (余談)C++/CXで用意している Windows Runtimeのスレッド スライド非表示ページ Platform::Agile<T> Windows Runtimeのクラスのインスタンス →どのスレッドからでもアクセス可能 普通はクライアント側で Platform::Agile<T> を利用する必要なし XAMLなどのUIスレッドはAgileは継承していない(マルチスレッ ドに出来ない) 一部を除いて © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Windows Store Apps demo
XAMLからのクリックイベント(1) <Button x:Name="SortButton" Content="Sort" Click="SortButton_Click"/>
別のスレッドプールを指定することも出来る XAMLからのクリックイベント(2) void ScenarioInput1::SortButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){ SortButton->IsEnabled = false; textblock->Text = "ソート中(*´Д`)..."; sortVector = ExtractInts( Textbox->Text ); create_task([this] { MergeSort( sortVector.begin(), sortVector.end() ); }).then( [this] { textblock->Text = MakeString( sortVector ); SortButton->IsEnabled = true; }, task_continuation_context::use_current()); } この場合の.thenは、 UIスレッドに返している 別のスレッドプールを指定することも出来る
(余談)Visual C++並列高速化演算 3/15/2017 (余談)Visual C++並列高速化演算 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
とりあえず動かす時には、 デバックなし実行[Ctrl+F5] 3/15/2017 C++AMP C++言語の拡張 現在のWindowsが動くOS環境であれば、問題なく 動作する模様 →Intel/AMD/NVIDIAなどのGPUでOK concurrency::parallel_for_each など とりあえず動かす時には、 デバックなし実行[Ctrl+F5] © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
3/15/2017 6. まとめ © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Summry C++11標準のタスク処理 +マイクロソフト提供のPPL タスク処理 がある! 3/15/2017 Summry C++11標準のタスク処理 +マイクロソフト提供のPPL タスク処理 がある! resumable/await は C#の async/await VC++ならconcurrency::task が便利 C++/CXならconcurrency::create_async がよし なにやってくれる これだけは覚えて帰ってね! © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
参考 Hilo での非同期プログラミング パターンとヒント (C++ と XAML を使っ た Windows ストア アプリ) http://msdn.microsoft.com/ja-jp/library/windows/apps/jj160321.aspx C++ における Windows ストア アプリ用の非同期操作の作成 http://msdn.microsoft.com/ja-jp/library/windows/apps/hh750082.aspx C++AMPを用いたGPUプログラミング http://www.aerospace.sd.tmu.ac.jp/hydrodynamics/main/colums/AMPinde x.html Windows 8 Asynchronous Operations in C++ with PPL http://code.msdn.microsoft.com/windowsapps/Windows-8-Asynchronous- 08009a0d