Skip to content

Commit f2fe86c

Browse files
committed
Timer: use a lock for all std::cout uses
1 parent 8739b68 commit f2fe86c

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/timer.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
#include <utility>
2424
#include <vector>
2525

26-
// TODO: print through a synchronized logger
27-
2826
namespace {
2927
using dataElementType = std::pair<std::string, struct TimerResultsData>;
3028
bool more_second_sec(const dataElementType& lhs, const dataElementType& rhs)
3129
{
3230
return lhs.second.seconds() > rhs.second.seconds();
3331
}
32+
33+
// TODO: remove and print through (synchronized) ErrorLogger instead
34+
std::mutex stdCoutLock;
3435
}
3536

3637
// TODO: this does not include any file context when SHOWTIME_FILE thus rendering it useless - should we include the logging with the progress logging?
@@ -43,13 +44,17 @@ void TimerResults::showResults(SHOWTIME_MODES mode) const
4344
TimerResultsData overallData;
4445
std::vector<dataElementType> data;
4546

46-
// lock the whole logging operation to avoid multiple threads printing their results at the same time
47-
std::lock_guard<std::mutex> l(mResultsSync);
47+
{
48+
std::lock_guard<std::mutex> l(mResultsSync);
4849

49-
data.reserve(mResults.size());
50-
data.insert(data.begin(), mResults.cbegin(), mResults.cend());
50+
data.reserve(mResults.size());
51+
data.insert(data.begin(), mResults.cbegin(), mResults.cend());
52+
}
5153
std::sort(data.begin(), data.end(), more_second_sec);
5254

55+
// lock the whole logging operation to avoid multiple threads printing their results at the same time
56+
std::lock_guard<std::mutex> l(stdCoutLock);
57+
5358
std::cout << std::endl;
5459

5560
size_t ordinal = 1; // maybe it would be nice to have an ordinal in output later!
@@ -115,7 +120,7 @@ void Timer::stop()
115120

116121
if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE) {
117122
const double sec = (double)diff / CLOCKS_PER_SEC;
118-
std::lock_guard<std::mutex> l(mCoutLock);
123+
std::lock_guard<std::mutex> l(stdCoutLock);
119124
std::cout << mStr << ": " << sec << "s" << std::endl;
120125
} else {
121126
if (mTimerResults)
@@ -125,5 +130,3 @@ void Timer::stop()
125130

126131
mStopped = true;
127132
}
128-
129-
std::mutex Timer::mCoutLock;

lib/timer.h

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

0 commit comments

Comments
 (0)