Skip to content

Instantly share code, notes, and snippets.

@sdi1982
Forked from KPB3rd/SimpleParallelForLoop
Created April 25, 2021 20:22
Show Gist options
  • Select an option

  • Save sdi1982/ba7b8532c8477fd79057848af10a3143 to your computer and use it in GitHub Desktop.

Select an option

Save sdi1982/ba7b8532c8477fd79057848af10a3143 to your computer and use it in GitHub Desktop.
Simple Parallel For Loop C++
// Initialize detector vector
if (targetDetectors.size() == 0)
{
for (unsigned iter = 0; iter < inputImages.size(); iter++) targetDetectors.push_back(CalibTargetDetector());
}
// Create Output vector
for (unsigned iter = 0; iter < inputImages.size(); iter++) targetImages.push_back(TypedObject<cv::Mat>(cv::Mat()));
std::vector<std::thread*> threads(inputImages.size());
for (int t = 0; t < threads.size(); t++)
{
threads[t] = new std::thread(&DetectTargetImpl::DetectTargetThreaded, this, t);
}
// Wait for threads to finish
std::for_each(threads.begin(), threads.end(), [](std::thread* x){ x->join(); });
// Free threads
for (unsigned iter = 0; iter < threads.size(); iter++) delete threads[iter];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment