-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainStdThread.cpp
More file actions
47 lines (36 loc) · 1.21 KB
/
mainStdThread.cpp
File metadata and controls
47 lines (36 loc) · 1.21 KB
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
44
45
46
47
#include <stdlib.h>
#include <string>
#include <iostream>
#include <vector>
#include <thread>
/** Define a simple function for each thread to run. */
uint64_t increment(uint64_t maxValue) {
uint64_t counter = 0;
while (++counter != maxValue) {
continue;
}
return counter;
}
/** Define the main entry point to the program. */
int main(int numArguments, char * const * const arguments) {
// Alias some of the types we will be using.
typedef std::thread * ThreadType;
typedef std::vector<ThreadType> ThreadVectorType;
uint64_t counter = 0;
uint64_t maxValue = 10000000000;
// Define the list of threads to capture the calculated values from each thread.
ThreadVectorType threadVector;
// Define the number of threads to run.
uint16_t numThreads = 4;
// Define our new threads, and corresponding futures.
for (size_t i = 0; i < numThreads; i++) {
threadVector.push_back(new std::thread(increment, maxValue/numThreads));
}
// Get the calculated values from each thread.
for (auto & curThread : threadVector) {
curThread->join();
}
std::cout << "Total: " << counter << std::endl;
std::cout << "Successful Completion!" << std::endl;
return EXIT_SUCCESS;
}