From 7b6293c06789c298686e7b305a6b54d479b9276b Mon Sep 17 00:00:00 2001 From: eidheim Date: Thu, 1 Aug 2019 09:13:08 +0200 Subject: [PATCH] Made Date::to_string thread safe with other Date::to_string calls --- utility.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/utility.hpp b/utility.hpp index 29b368a..d96d0e9 100644 --- a/utility.hpp +++ b/utility.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -320,11 +321,15 @@ namespace SimpleWeb { class Date { public: /// Returns the given std::chrono::system_clock::time_point as a string with the following format: Wed, 31 Jul 2019 11:34:23 GMT. - /// Warning: this function uses std::gmtime and is thus not thread safe. + /// Warning: while this function is thread safe with other Date::to_string() calls, + /// it is not thread safe with other functions that include calls to std::gmtime. static std::string to_string(const std::chrono::system_clock::time_point time_point) noexcept { static std::string result_cache; static std::chrono::system_clock::time_point last_time_point; + static std::mutex mutex; + std::lock_guard lock(mutex); + if(std::chrono::duration_cast(time_point - last_time_point).count() == 0 && !result_cache.empty()) return result_cache;