Skip to content

Commit 8739b68

Browse files
committed
fixed TSAN errors in Timer::stop() with --showtime=file and multiple threads
1 parent ff37e06 commit 8739b68

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

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)
@@ -113,6 +115,7 @@ void Timer::stop()
113115

114116
if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE) {
115117
const double sec = (double)diff / CLOCKS_PER_SEC;
118+
std::lock_guard<std::mutex> l(mCoutLock);
116119
std::cout << mStr << ": " << sec << "s" << std::endl;
117120
} else {
118121
if (mTimerResults)
@@ -122,3 +125,5 @@ void Timer::stop()
122125

123126
mStopped = true;
124127
}
128+
129+
std::mutex Timer::mCoutLock;

lib/timer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class CPPCHECKLIB Timer {
8686
std::clock_t mStart;
8787
const SHOWTIME_MODES mShowTimeMode;
8888
bool mStopped;
89+
static std::mutex mCoutLock;
8990
};
9091
//---------------------------------------------------------------------------
9192
#endif // timerH

0 commit comments

Comments
 (0)