C++/CLIでParallel::ForEachを使う方法

huahi11115 640 評価のポイント
2026-06-28T01:56:25.2+00:00

下のC++/CLIのコードを、Parallel::ForEachを使用して書き替えて下さい。

インクルードファイルにも、何を指定したら良いのか分かりません。

生成AIにも質問しましたが、有効な回答が得られませんでした。

List<List<int>^>^ List0 = gcnew List<List<int>^>(4);

void test1(){
	for each(l1 in List0) {//Parallel::ForEachを使いたい
		int dummy=0;
	}
}

開発者テクノロジ | C++
開発者テクノロジ | C++

C プログラミング言語の拡張機能として作成された高レベルの汎用プログラミング言語。低レベルのメモリ操作機能に加えて、オブジェクト指向、汎用、関数型の機能を備えています。


質問作成者が受け入れた回答

motosan 3,310 評価のポイント
2026-06-28T08:31:19.01+00:00

私はあまり詳しくありませんが、下記のやり方でビルドできました。

  • Visual Studio 2026 (18.7.2)
  • [プロジェクト テンプレート] … CLR コンソールアプリ(.NET Framework)

下記の修正をしました。(2026/7/2)

  • #include "pch.h"は不要なので削除しました。
  • function は非同期に処理されるため、Listに要素を追加する処理は競合するため単純なfor文に置き換えました。
  • typedef は使わないように修正しました。

#include "pch.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Threading::Tasks;

typedef List<int>^ datatype_t;
ref struct mystruct {
    List<datatype_t>^ m_a;
public:
    mystruct(List<datatype_t>^ dst) {
        m_a = dst;
    }
    void function(int number) {
        List<int>^ numbers = gcnew List<int>();
        numbers->Add(10 + number);

        m_a->Add(numbers);
    }
    void function2(datatype_t d) {
        for (int j = 0; j < d->Count; j++) {
            System::Console::WriteLine(
                System::String::Format("d[{0}] = {1}", j, d[j]));
        }
    }
};

int main(array<System::String ^> ^args)
{
    List<datatype_t>^ List0 = gcnew List<datatype_t>(10);

    mystruct^ plfor = gcnew mystruct(List0);

    Parallel::For(
        0,
        10,
        gcnew Action<int>(plfor, &mystruct::function)
    );
    Parallel::ForEach<datatype_t>(
        List0,
        gcnew Action<datatype_t>(plfor, &mystruct::function2)
    );

    return 0;
}
  • using namespace で必要な名前空間を読み込んでおく。
  • typedef でアイテムの型を定義しておく。
  • mystruct でデータと関数を定義しておく。
  • Parallel::ForEachで指定するアクションをmystructの関数で定義している。

下記の記事を参考にしました。

C++CLIでParallel::For | ぬの部屋(仮) https://suzulang.com/ccli%e3%81%a7parallelfor/.

huahi11115 さんが期待しているコードではないかもしれませんが、ご参考までに

この回答は役に立ちましたか?

1 人がこの回答が役に立ったと思いました。
0 件のコメント コメントはありません

1 件の追加の回答

並べ替え方法: 最も役に立つ
  1. gekka 14,146 評価のポイント MVP ボランティア モデレーター
    2026-06-29T09:15:10.66+00:00

    こんな

    #pragma once
    
    #include <iostream>
    
    using namespace System;
    using namespace System::Collections::Generic;
    using namespace System::Threading::Tasks;
    
    //グローバル定義でループ用の処理を定義
    static inline void g_innerLoop(int source, ParallelLoopState^ state)
    {
    	std::cout << source << std::endl;
    };
    
    static inline void g_outerLoop(List<int>^ source, ParallelLoopState^ state)
    {
    	auto a = gcnew System::Action<int, ParallelLoopState^ >(g_innerLoop);
    	auto result = System::Threading::Tasks::Parallel::ForEach<int>(source, a);
    };
    
    namespace CPPLibraryCLICore {
    
    	public ref class TestClass {
    
    
    		//メンバで定義でループ用の処理を定義
    		void innerLoop(int source, ParallelLoopState^ state)
    		{
    			std::cout << source << std::endl;
    		};
    
    		void outerLoop(List<int>^ source, ParallelLoopState^ state)
    		{
    			auto a = gcnew System::Action<int, ParallelLoopState^ >(this, &TestClass::innerLoop);
    			auto result = System::Threading::Tasks::Parallel::ForEach<int>(source, a);
    		};
    
    	public:
    		void Test() {
    			auto list = gcnew List<List<int>^>();
    			for (int i = 0; i < 100; i++) {
    				auto inner = gcnew List<int>(0);
    
    				for (int k = 0; k < 100; k++) {
    					inner->Add(k);
    				}
    				list->Add(inner);
    			}
    
    
    			std::cout << "===========" << std::endl;
    
    
    			// グローバル関数でループ
    			auto a1 = gcnew System::Action< List<int>^, ParallelLoopState^ >(::g_outerLoop);
    			auto result1 = System::Threading::Tasks::Parallel::ForEach< List<int>^>(list, a1);
    
    			std::cout << "===========" << std::endl;
    
    
    			//refクラスメンバ関数でループ
    			auto a2 = gcnew System::Action< List<int>^, ParallelLoopState^ >(this, &TestClass::outerLoop);
    			auto result2 = System::Threading::Tasks::Parallel::ForEach< List<int>^>(list, a2);
    
    			return;
    		}
    	};
    }
    

    この回答は役に立ちましたか?

    0 件のコメント コメントはありません

お客様の回答

質問作成者は回答に "承認済み"、モデレーターは "おすすめ" とマークできます。これにより、ユーザーは作成者の問題が回答によって解決したことを把握できます。