Skip to content

Commit 2b9cc26

Browse files
Make send thread safe
1 parent 6efdb16 commit 2b9cc26

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

include/cpp-statsd-client/StatsdClient.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <cstdio>
77
#include <iomanip>
88
#include <memory>
9+
#include <mutex>
910
#include <random>
1011
#include <sstream>
1112
#include <string>
@@ -165,6 +166,9 @@ class StatsdClient {
165166
//! The buffer string format our stats before sending them
166167
mutable std::string m_buffer;
167168

169+
//! The mutex to lock m_buffer
170+
mutable std::mutex m_buffer_mutex;
171+
168172
//! Fixed floating point precision of gauges
169173
int m_gaugePrecision;
170174
};
@@ -291,6 +295,7 @@ inline void StatsdClient::send(const std::string& key,
291295
std::stringstream valueStream;
292296
valueStream << std::fixed << std::setprecision(m_gaugePrecision) << value;
293297

298+
std::lock_guard<std::mutex> buffer_lock(m_buffer_mutex);
294299
m_buffer.clear();
295300

296301
m_buffer.append(m_prefix);

0 commit comments

Comments
 (0)