View difference between Paste ID: 6UNAcSHw and Q28w0k4b
SHOW: | | - or go back to the newest paste.
1
#include <atomic>
2
3
#define WORKLOAD (500000)
4
5
class Work
6
{
7
public:
8
	int DataA[WORKLOAD];
9
	int DataB[WORKLOAD];
10
	int DataC[WORKLOAD];
11
12
	std::atomic<size_t> Updates = 0;
13
14
	Work()
15
	{
16
		for (int i = 0; i < WORKLOAD; ++i)
17
		{
18
			DataA[i] = 10;
19
			DataB[i] = 10;
20
			DataC[i] = 10;
21
		}
22
	}
23
24
	void Do()
25
	{
26
		for (int i = 0; i < WORKLOAD; ++i)
27
		{
28
			DataC[i] = DataC[i] + DataA[i] + DataB[i];
29
		}
30
		Updates.fetch_add(1);
31
	}
32
33
};
34
35
bool Run = true;
36
#define T_CNT (1)
37
ThreadPool Threads;
38
Work WorlObj;
39
40
void WorkerFunction()
41
{
42
	ThreadPool::Progress LocalProgress = 0;
43
	while (Run)
44
	{
45
		WorlObj.Do();
46
		Threads.Sync(LocalProgress);
47
		WorlObj.Do();
48
		Threads.Sync(LocalProgress);
49
	}
50
}
51
52
int main()
53
{
54
	//Test einstellen
55
	int UseThreads = 4;
56
	int TestTime = 50;
57
	int TestLoops = 100;
58
59
	printf("testing... need %i ms\n", TestLoops* TestTime);
60
61
	//threads starten
62
	Threads.Start<std::function<void()>>([&](){ WorkerFunction(); }, UseThreads);
63
64
	//schenllsten finden
65
	WorlObj.Updates = 0;
66
	int MaxUpdate = 0;
67
	while (TestLoops > 0)
68
	{
69
		--TestLoops;
70
		Sleep(TestTime);
71
		if (WorlObj.Updates > MaxUpdate)
72
			MaxUpdate = WorlObj.Updates;
73
		WorlObj.Updates = 0;
74
	}
75
76
	//ausgabe
77
	printf("MaxUpdate: %i\n", MaxUpdate);
78
79
	//beenden
80
	Run = false;
81
	Threads.Join();
82
	std::getchar();
83
84
	return 0;
85
}