-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadLines.h
43 lines (35 loc) · 928 Bytes
/
threadLines.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
#include <math.h>
#include <thread>
template <class T, class ArgObject> void threadLines(T* func, ArgObject context, int nbLines, int nbThread)
{
if (nbThread > 1)
{
std::thread threadList[64];
if (nbThread > 64)
nbThread = 64;
if (nbLines < nbThread)
{
nbThread = nbLines;
}
float linesPerThread = nbLines / nbThread;
int startY = 0;
int stopY;
for (int i = 0; i < nbThread; i++)
{
stopY = startY + (int)ceilf(linesPerThread);
if (stopY > nbLines - 1)
stopY = nbLines - 1;
threadList[i] = std::thread(func, context, startY, stopY);
startY = stopY + 1;
}
for (int i = 0; i < nbThread; i++)
{
threadList[i].join();
}
}
else
{
(*func)(context, 0, nbLines - 1);
}
}