This repository was archived by the owner on May 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMain.cpp
More file actions
60 lines (48 loc) · 1.81 KB
/
Main.cpp
File metadata and controls
60 lines (48 loc) · 1.81 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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <gflags/gflags.h>
#include "Mixer.h"
DEFINE_int32(num_producers, 100, "number of producers to run");
DEFINE_int32(num_threads, 1, "number of threads to run");
using std::shared_ptr;
using std::vector;
void createAndRunMixer(vector<shared_ptr<Producer>> producers, int me,
vector<shared_ptr<ToFreeQueue>> toFreeQueues) {
Mixer m(producers, FLAGS_num_producers, me, toFreeQueues);
m.run();
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
// Initialize producers
vector<shared_ptr<Producer>> producers;
producers.push_back(
shared_ptr<SimpleProducer>(new SimpleProducer(8, 100000)));
producers.push_back(shared_ptr<VectorProducer>(
new VectorProducer(100000, std::chrono::duration<double>(1.0))));
// Set up a work queue for each thread
vector<std::thread> threads;
vector<shared_ptr<ToFreeQueue>> toFreeQueues;
for (int i = 0; i < FLAGS_num_threads; i++) {
auto toFreeQ = shared_ptr<ToFreeQueue>(new ToFreeQueue());
toFreeQueues.push_back(toFreeQ);
}
for (int i = 0; i < FLAGS_num_threads; i++) {
// each thread gets an arbitrary id given by [i]
threads.push_back(
std::thread(createAndRunMixer, producers, i, toFreeQueues));
}
using namespace std::chrono;
high_resolution_clock::time_point beginTime = high_resolution_clock::now();
for (auto& t : threads) {
t.join();
}
// Cleanup any remaining memory
for (int i = 0; i < FLAGS_num_threads; i++) {
toFreeQueues[i]->freeIgnoreLifetime();
}
high_resolution_clock::time_point endTime = high_resolution_clock::now();
duration<double> span = duration_cast<duration<double>>(endTime - beginTime);
std::cout << "Elapsed time: " << span.count() << std::endl;
}