|
1 | 1 | #include <chrono>
|
2 | 2 | #include <iostream>
|
| 3 | +#include <thread> |
3 | 4 | #include <vector>
|
4 | 5 |
|
5 | 6 | #include <gflags/gflags.h>
|
6 | 7 |
|
7 | 8 | #include "Mixer.h"
|
8 | 9 |
|
9 | 10 | DEFINE_int32(num_producers, 100, "number of producers to run");
|
| 11 | +DEFINE_int32(num_threads, 1, "number of threads to run"); |
10 | 12 |
|
11 |
| -int main(int argc, char **argv) { |
| 13 | +using std::make_shared; |
| 14 | +using std::shared_ptr; |
| 15 | +using std::vector; |
| 16 | + |
| 17 | +void createAndRunMixer(vector<shared_ptr<Producer>> producers, int me, |
| 18 | + vector<shared_ptr<ToFreeQueue>> toFreeQueues) { |
| 19 | + Mixer m(producers, FLAGS_num_producers, me, toFreeQueues); |
| 20 | + m.run(); |
| 21 | +} |
12 | 22 |
|
| 23 | +int main(int argc, char **argv) { |
13 | 24 | gflags::ParseCommandLineFlags(&argc, &argv, true);
|
14 | 25 |
|
15 |
| - vector<std::unique_ptr<Producer>> producers; |
16 |
| - producers.push_back(std::move(std::make_unique<SimpleProducer>(8, 100000))); |
17 |
| - producers.push_back(std::move(std::make_unique<VectorProducer>( |
18 |
| - 100000, std::chrono::duration<double>(1.0)))); |
| 26 | + // Initialize producers |
| 27 | + vector<shared_ptr<Producer>> producers; |
| 28 | + producers.push_back(make_shared<SimpleProducer>(8, 100000)); |
| 29 | + producers.push_back( |
| 30 | + make_shared<VectorProducer>(100000, std::chrono::duration<double>(1.0))); |
| 31 | + |
| 32 | + // Set up a work queue for each thread |
| 33 | + vector<std::thread> threads; |
| 34 | + vector<shared_ptr<ToFreeQueue>> toFreeQueues; |
| 35 | + for (int i = 0; i < FLAGS_num_threads; i++) { |
| 36 | + shared_ptr<ToFreeQueue> toFreeQ = make_shared<ToFreeQueue>(); |
| 37 | + toFreeQueues.push_back(toFreeQ); |
| 38 | + } |
| 39 | + |
| 40 | + for (int i = 0; i < FLAGS_num_threads; i++) { |
| 41 | + // each thread gets an arbitrary id given by [i] |
| 42 | + threads.push_back( |
| 43 | + std::thread(createAndRunMixer, producers, i, toFreeQueues)); |
| 44 | + } |
19 | 45 |
|
20 | 46 | using namespace std::chrono;
|
21 | 47 |
|
22 |
| - Mixer m(std::move(producers), FLAGS_num_producers); |
23 |
| - |
24 | 48 | high_resolution_clock::time_point beginTime = high_resolution_clock::now();
|
25 |
| - m.run(); |
| 49 | + for (auto it = begin(threads); it != end(threads); ++it) { |
| 50 | + it->join(); |
| 51 | + } |
| 52 | + // Cleanup any remaining memory |
| 53 | + for (int i = 0; i < FLAGS_num_threads; i++) { |
| 54 | + toFreeQueues[i]->freeIgnoreLifetime(); |
| 55 | + } |
26 | 56 | high_resolution_clock::time_point endTime = high_resolution_clock::now();
|
27 | 57 |
|
28 | 58 | duration<double> span = duration_cast<duration<double>>(endTime - beginTime);
|
|
0 commit comments