OpenMP Parallel For Loop
此示例顯示如何將迴圈劃分為相等的部分並並行執行它們。
// Splits element vector into element.size() / Thread Qty
// and allocate that range for each thread.
#pragma omp parallel for
for (size_t i = 0; i < element.size(); ++i)
element[i] = ...
// Example Allocation (100 element per thread)
// Thread 1 : 0 ~ 99
// Thread 2 : 100 ~ 199
// Thread 2 : 200 ~ 299
// ...
// Continue process
// Only when all threads completed their allocated
// loop job
...
*請特別注意不要修改並行 for 迴圈使用的向量的大小,因為分配的範圍索引不會自動更新。