Skip to content

Commit ed900c0

Browse files
committed
fix race conditions, add -fsanitize=thread -g to discover them
1 parent 5338c3a commit ed900c0

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
#include "utils/utils.hpp"
77
#include <fcntl.h>
88
#include <boost/program_options.hpp>
9+
#include <atomic>
910
namespace po = boost::program_options;
1011

1112
using namespace std;
12-
bool continue_reading = true;
13+
atomic<bool> continue_reading = {true};
1314

1415
void handle_printing(Summarizer *summarizer, int delay, int precision)
1516
{

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();

0 commit comments

Comments
 (0)