Skip to content

Commit b816a93

Browse files
committed
fixed TSAN errors in Timer::stop() with --showtime=file and multiple threads
1 parent 68ac8a2 commit b816a93

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

lib/timer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <utility>
2424
#include <vector>
2525

26+
// TODO: print through a synchronized logger
27+
2628
namespace {
2729
using dataElementType = std::pair<std::string, struct TimerResultsData>;
2830
bool more_second_sec(const dataElementType& lhs, const dataElementType& rhs)
@@ -122,6 +124,7 @@ void Timer::stop()
122124

123125
if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE) {
124126
const double sec = (double)diff / CLOCKS_PER_SEC;
127+
std::lock_guard<std::mutex> l(mCoutLock);
125128
std::cout << mStr << ": " << sec << "s" << std::endl;
126129
} else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) {
127130
const double sec = (double)diff / CLOCKS_PER_SEC;
@@ -134,3 +137,5 @@ void Timer::stop()
134137

135138
mStopped = true;
136139
}
140+
141+
std::mutex Timer::mCoutLock;

lib/timer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class CPPCHECKLIB Timer {
8888
std::clock_t mStart;
8989
const SHOWTIME_MODES mShowTimeMode;
9090
bool mStopped;
91+
static std::mutex mCoutLock;
9192
};
9293
//---------------------------------------------------------------------------
9394
#endif // timerH

0 commit comments

Comments
 (0)