Added mutex

This commit is contained in:
tastytea 2018-05-17 17:59:44 +02:00
parent 56cfe7e2ce
commit 273a92c4ab
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
4 changed files with 21 additions and 3 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp
VERSION 0.12.2
VERSION 0.12.3
LANGUAGES CXX
)

View File

@ -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

View File

@ -83,7 +83,7 @@ const uint_fast16_t API::http::request(const method &meth,
request.setOpt<curlopts::HttpHeader>(headers);
request.setOpt<curlopts::FollowLocation>(true);
request.setOpt<curlpp::options::WriteFunction>
request.setOpt<curlopts::WriteFunction>
(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<std::mutex> 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;
}

View File

@ -23,6 +23,7 @@
#include <map>
#include <memory>
#include <array>
#include <mutex>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
@ -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);