スポンサーリンク

C++CLIでParallel::For

C#のサンプルは沢山あるがCLIで実行できずに困ったのでここに書いておく。

下記は、要素数10のdoubleの配列aの内容を、0~9の1.5倍の値で初期化する。

 

using namespace System;
typedef double datatype_t;
ref struct mystruct{
   array<datatype_t>^ m_a;
public:
   mystruct(array<datatype_t>^ dst){
      m_a = dst;
   }
   void function(int number){
      m_a[ number] =  number*1.5;
   }
};
int main(array<System::String ^> ^args)
{
   array<datatype_t>^ a = gcnew array<datatype_t>(10);

   mystruct^ plfor = gcnew mystruct(a);

   System::Threading::Tasks::Parallel::For(
      0,
      a->Length,
      gcnew Action<int>(plfor, &mystruct::function)
   );

   for(int i= 0 ;i < a->Length;i++){
      System::Console::WriteLine( 
         System::String::Format("a[{0}] = {1}",i,a[i]));
   }

   Console::ReadLine();

    return 0;
}

 

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: