Commit aa55908d authored by octal's avatar octal

Fixed thread-safety of rest_server example

parent c6fc42b2
...@@ -66,6 +66,8 @@ private: ...@@ -66,6 +66,8 @@ private:
void doRecordMetric(const Rest::Request& request, Net::Http::ResponseWriter response) { void doRecordMetric(const Rest::Request& request, Net::Http::ResponseWriter response) {
auto name = request.param(":name").as<std::string>(); auto name = request.param(":name").as<std::string>();
Guard guard(metricsLock);
auto it = std::find_if(metrics.begin(), metrics.end(), [&](const Metric& metric) { auto it = std::find_if(metrics.begin(), metrics.end(), [&](const Metric& metric) {
return metric.name() == name; return metric.name() == name;
}); });
...@@ -90,6 +92,8 @@ private: ...@@ -90,6 +92,8 @@ private:
void doGetMetric(const Rest::Request& request, Net::Http::ResponseWriter response) { void doGetMetric(const Rest::Request& request, Net::Http::ResponseWriter response) {
auto name = request.param(":name").as<std::string>(); auto name = request.param(":name").as<std::string>();
Guard guard(metricsLock);
auto it = std::find_if(metrics.begin(), metrics.end(), [&](const Metric& metric) { auto it = std::find_if(metrics.begin(), metrics.end(), [&](const Metric& metric) {
return metric.name() == name; return metric.name() == name;
}); });
...@@ -135,6 +139,9 @@ private: ...@@ -135,6 +139,9 @@ private:
int value_; int value_;
}; };
typedef std::mutex Lock;
typedef std::lock_guard<Lock> Guard;
Lock metricsLock;
std::vector<Metric> metrics; std::vector<Metric> metrics;
std::shared_ptr<Net::Http::Endpoint> httpEndpoint; std::shared_ptr<Net::Http::Endpoint> httpEndpoint;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment