Skip to content

Commit b74fef1

Browse files
authored
Merge pull request #2 from ahmedakef/apply_skeeto_comments
Apply skeeto comments
2 parents 5338c3a + faa6654 commit b74fef1

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

main.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22
#include <sstream>
33
#include <vector>
44
#include <thread>
5+
#include <mutex>
6+
#include <condition_variable>
7+
#include <fcntl.h>
58
#include "modules/summarizer.hpp"
69
#include "utils/utils.hpp"
7-
#include <fcntl.h>
810
#include <boost/program_options.hpp>
11+
#include <atomic>
912
namespace po = boost::program_options;
1013

1114
using namespace std;
15+
mutex continue_m;
16+
condition_variable continue_cv;
1217
bool continue_reading = true;
1318

1419
void handle_printing(Summarizer *summarizer, int delay, int precision)
1520
{
1621
print_elements(vector<string>{"Count", "Mean", "Min", "Max", "P95", "P99", "\n"}, precision);
22+
unique_lock<mutex> lock(continue_m);
1723
while (true)
1824
{
1925
cout << "\r";
@@ -23,31 +29,35 @@ void handle_printing(Summarizer *summarizer, int delay, int precision)
2329
cout << endl;
2430
break;
2531
}
26-
this_thread::sleep_for(chrono::seconds(delay));
32+
// Atomically releases lock, blocks the current executing thread.
33+
// The thread will be unblocked when notify_one() is executed, or when the delay expires.
34+
// When unblocked, regardless of the reason, lock is reacquired
35+
continue_cv.wait_for(lock, chrono::seconds(delay));
2736
}
2837
}
2938

3039
void start(int delay, int precision)
3140
{
32-
int number;
3341
string line;
3442
Summarizer summarizer;
3543

3644
thread timer(handle_printing, &summarizer, delay, precision);
3745

38-
while (continue_reading)
46+
while (true)
3947
{
4048
if (!getline(cin, line))
4149
{
4250
// either an error hapened or we reached EOF
51+
lock_guard<mutex> lock(continue_m);
4352
continue_reading = false;
53+
continue_cv.notify_one();
4454
break;
4555
}
46-
if (!is_number(line) || line == "")
56+
auto [number, ok] = is_number(line);
57+
if (!ok || line == "")
4758
{
4859
continue;
4960
}
50-
stringstream(line) >> number;
5161
summarizer.add_number(number);
5262
}
5363

makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ all:
1111
# all:
1212
# c++ \
1313
# -std=c++11 \
14+
# -fsanitize=thread -g \
1415
# -pthread \
1516
# -o summarize \
1617
# main.cpp utils/utils.cpp modules/summarizer.cpp \

modules/summarizer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ Summarizer::Summarizer() : acc(tag::extended_p_square::probabilities = probs) {}
1212

1313
void Summarizer::add_number(double number)
1414
{
15+
std::lock_guard<std::mutex> lock(this->lock);
16+
1517
acc(number);
1618
}
1719

1820
void Summarizer::print_summary(int precision)
1921
{
22+
std::lock_guard<std::mutex> lock(this->lock);
23+
2024
double count_v = count(acc);
2125
if (count_v == 0.0){
2226
print_elements(std::vector<double>{0, 0, 0, 0, 0, 0}, precision);

modules/summarizer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef Summarizer_H
22
#define Summarizer_H
3+
#include <mutex>
34
#include <boost/accumulators/accumulators.hpp>
45
#include <boost/accumulators/statistics/stats.hpp>
56
#include <boost/accumulators/statistics/count.hpp>
@@ -15,6 +16,8 @@ typedef accumulator_set<double, stats<tag::count, tag::mean, tag::min, tag::max,
1516
class Summarizer
1617
{
1718
accumulator_t acc;
19+
std::mutex lock;
20+
1821

1922
public:
2023
Summarizer();

utils/utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ void print_elements(const vector<T> &elements, int precision)
2222
template void print_elements(const vector<double> &elements, int precision);
2323
template void print_elements(const vector<string> &elements, int precision);
2424

25-
bool is_number(const string &s)
25+
tuple<double, bool> is_number(const string &s)
2626
{
2727
char *end;
28-
strtod(s.c_str(), &end);
29-
return *end == 0;
28+
double r = strtod(s.c_str(), &end);
29+
return {r, *end == 0};
3030
}

utils/utils.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
#include <vector>
55
#include <string>
6+
#include <tuple>
67

78
template <typename T>
89
void print_element(T &t, int precision);
910

1011
template <typename T>
1112
void print_elements(const std::vector<T> &elements, int precision);
1213

13-
bool is_number(const std::string &s);
14+
std::tuple<double, bool> is_number(const std::string &s);
1415

1516
#endif

0 commit comments

Comments
 (0)