updated examples with mutexes
This commit is contained in:
parent
e7d1808f85
commit
e51841fe20
|
@ -9,6 +9,7 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
#ifdef MASTODON_CPP
|
#ifdef MASTODON_CPP
|
||||||
#include "mastodon-cpp.hpp"
|
#include "mastodon-cpp.hpp"
|
||||||
#else
|
#else
|
||||||
|
@ -39,16 +40,20 @@ int main(int argc, char *argv[])
|
||||||
std::uint8_t counter = 0;
|
std::uint8_t counter = 0;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
++counter;
|
|
||||||
std::cout << answer;
|
|
||||||
answer.clear();
|
|
||||||
if (counter == 10)
|
|
||||||
{
|
|
||||||
std::cerr << "Cancelling...\n";
|
|
||||||
ptr->cancel_stream();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
|
if (ptr != nullptr)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(ptr->get_mutex());
|
||||||
|
++counter;
|
||||||
|
std::cout << answer;
|
||||||
|
answer.clear();
|
||||||
|
if (counter == 10)
|
||||||
|
{
|
||||||
|
std::cerr << "Cancelling...\n";
|
||||||
|
ptr->cancel_stream();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pub.join();
|
pub.join();
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
// If we are compiling mastodon-cpp, use another include path
|
// If we are compiling mastodon-cpp, use another include path
|
||||||
#ifdef MASTODON_CPP
|
#ifdef MASTODON_CPP
|
||||||
|
@ -51,6 +52,16 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
// Skip iteration if ptr points not to the Mastodon::API::http object
|
||||||
|
if (ptr != nullptr)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Acquire lock to for the stream variable
|
||||||
|
std::lock_guard<std::mutex> lock(ptr->get_mutex());
|
||||||
|
|
||||||
// Parse event stream and clear it afterwards
|
// Parse event stream and clear it afterwards
|
||||||
std::vector<Easy::stream_event> events = Easy::parse_stream(stream);
|
std::vector<Easy::stream_event> events = Easy::parse_stream(stream);
|
||||||
stream.clear();
|
stream.clear();
|
||||||
|
@ -85,7 +96,6 @@ int main(int argc, char *argv[])
|
||||||
cout << "Something undefined happened. 😱\n";
|
cout << "Something undefined happened. 😱\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub_tl.join();
|
pub_tl.join();
|
||||||
|
|
Reference in New Issue