2017-05-29 166 views
-6
Parallel.For(<your starting value >,<End criteria for loop>, delegate(int < your variable Name>) 
{ 
    // Your own code 
});  

上面我在C#中顯示了一些示例代碼。我想在C++/CLI中使用類似的功能,但我不知道如何使用這個表達式:「委託(int <您的變量名稱>)」。在C++/cli中並行for循環

+0

['的std :: for_each'](http://en.cppreference.com/w/cpp/algorithm/for_each)與即將發佈的C++中的'parallel_policy' [執行策略](http://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t) 17標準應該可以做到。 –

+6

所以什麼阻止你? 'Parallel.For'在* managed * C++中可用。 –

+2

如果您使用Windows/VisualStudio,請嘗試使用Concurrency :: parallel_for – yms

回答

0

如果您正在使用C++ CLI,那麼你應該能夠使用在C#中使用相同的Parallel.For因爲System.Threading.Tasks.Parallel是一個普通的.NET Framework類

例(未經測試,甚至沒有編譯):

ref class SomeClass 
{ 
public: 
    static void Func(int index) 
    { 
     Console::WriteLine("Test {0}", index); 
    } 
}; 

delegate void MyCallback(int index); 

int main() 
{ 
    MyCallback^ callback = gcnew MyCallback(SomeClass::Func);  
    Parallel.For(0, 9, callback); 
} 

相關:How to: Define and Use Delegates in C++/CLI