From 273a92c4abf24a346d994fd171d67862a452ee35 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 17 May 2018 17:59:44 +0200 Subject: [PATCH] Added mutex --- CMakeLists.txt | 2 +- README.md | 2 +- src/http.cpp | 8 +++++++- src/mastodon-cpp.hpp | 12 ++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd7643e..428c8b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.12.2 + VERSION 0.12.3 LANGUAGES CXX ) diff --git a/README.md b/README.md index 031a8aa..936eec1 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ Run `make package` from the build directory to generate a tar.gz archive. # Status of implementation -Feature complete as of Mastodon 2.3.0 +Feature complete as of Mastodon 2.4.0 * [x] GET /api/v1/accounts/:id * [x] GET /api/v1/accounts/verify_credentials diff --git a/src/http.cpp b/src/http.cpp index 5d5f39a..6095cc6 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -83,7 +83,7 @@ const uint_fast16_t API::http::request(const method &meth, request.setOpt(headers); request.setOpt(true); - request.setOpt + request.setOpt (std::bind(&http::callback, this, _1, _2, _3, &answer)); if (!formdata.empty()) { @@ -176,6 +176,7 @@ const void API::http::get_headers(string &headers) const const size_t API::http::callback(char* data, size_t size, size_t nmemb, string *str) { + std::lock_guard lock(_mutex); if (_cancel_stream) { // This throws the runtime error: Failed writing body @@ -195,3 +196,8 @@ const void API::http::abort_stream() { cancel_stream(); } + +std::mutex &API::http::get_mutex() +{ + return _mutex; +} diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 6ffb67b..2730b63 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -135,12 +136,23 @@ public: [[deprecated("Will vanish in 1.0.0. Use cancel_stream() instead.")]] const void abort_stream(); + /*! + * @brief Gets the mutex guarding the string that is written to. + * + * The mutex guards the function that writes to the string you + * specified in get_stream(). + * + * @return A reference of the mutex. + */ + std::mutex &get_mutex(); + private: const API &parent; const string _instance; const string _access_token; string _headers; bool _cancel_stream; + std::mutex _mutex; const size_t callback(char* data, size_t size, size_t nmemb, string *oss);