Upload
dinhdat
View
238
Download
8
Embed Size (px)
Citation preview
C++14
auto and decltype(auto) return types
Generic lambdas (一部)
C++17 (予定) Concurrency TS(?)
Resumable functions and await (一部)
C#のasync/awaitを意識
1つプロセス内の処理
複数プロセス間の処理
他リソース間の処理
非同期処理
レスポンスタイムの向上→「待たない」並行処理
マルチスレッドの効率的利用→並列処理
スループット向上
ex)ディスクアクセスの間に、CPUを利用する
レスポンスタイムの向上
ex)DBからデータを取得するなど、「非同期」でよく使われる例はこれ
1つプロセス内の処理
複数プロセス間の処理
他リソース間の処理
非同期処理
レスポンスタイムの向上→「待たない」並行処理
マルチスレッドの効率的利用→並列処理
スループット向上
ex)ディスクアクセスの間に、CPUを利用する
レスポンスタイムの向上
ex)DBからデータを取得するなど、「非同期」でよく使われる例はこれ
本日は、用語をこう定義して進めます1 → 非同期処理
2 → 並列高速演算
プロジェクト例
種類 機能の例
非同期処理
C++C++11
<future>
std::thread / std::promise
std::async
VC++
Windows
Runtime
C++/CX
ppltasks.hconcurrency::task
並列高速演算
DirectX
(GPGPU利用)
C++AMP
amp.hconcurrency::parallel_for_each
stdio.h
cout “Hello World!” endl
auto
cout “Hello World!” endl
// 代入されたラムダを関数呼び出し
[ ]( std::string const &str ) // 引数
{ std::cout << str << std::endl; } // コード
( “Hello World!” ); // 呼び出し
auto auto auto
return
plus( std::string( "Hello “ ), std::string( "World“ ));
#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と同じ形
concurency:: <int
concurency::
concurency::
型は合わせること
.then で返り値を別のタスクに渡す
どんどんつなげることが可能
concurency::task<int> t([](){
return( 1 );
}).then([](int n){
return( ++n );
}).then([](int n){
return( ++n );
})
そういえばC#でも似たような書き方出来ましたね
#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();
}
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 = "ファイル作成しました";});
}
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 = "ファイル作成しました";
}
App container ( package manifest )
UI
controls
( XAML )
相互互換のためのC++言語拡張機能(中間の変換無し)
C++実装Class
(ネイティブ)VSプロジェクトテンプレート
C++/CX
concurrency::create_async
App container ( package manifest )
UI controls
( XAML )C++/CX
C++実装クラス(ネイティブ)
Windows::Foundation::IAsyncActionなど
concurrency::task他のWindows
Runtimeと非同期通信
処理 利用クラス/アルゴリズム/メソッド
非同期タスク処理 concurrency::task
並列に分割した処理のグルーピング
concurrency::task_group
concurrency::structured_task_group
タスク処理の継続 concurrency::task::then
非同期アクション IAsyncAction
非同期進行状況 IAsyncActionWithProgress<TProgress>
結果を返す非同期 IAsyncOperation<TResult>
結果も返すし進行も判る
IAsyncOperationWithProgress<TResult,
TProgress>
処理 利用クラス
非同期タスク処理 concurrency::create_async
この場合の.thenは、UIスレッドに返している
別のスレッドプールを指定することも出来る
http://msdn.microsoft.com/ja-jp/library/windows/apps/jj160321.aspx
http://msdn.microsoft.com/ja-jp/library/windows/apps/hh750082.aspx
http://www.aerospace.sd.tmu.ac.jp/hydrodynamics/main/colums/AMPindex.html
http://code.msdn.microsoft.com/windowsapps/Windows-8-Asynchronous-08009a0d