diff --git a/examples/example09_streaming_api.cpp b/examples/example09_streaming_api.cpp index af6f2d6..3f8d7c3 100644 --- a/examples/example09_streaming_api.cpp +++ b/examples/example09_streaming_api.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef MASTODON_CPP #include "mastodon-cpp.hpp" #else @@ -39,16 +40,20 @@ int main(int argc, char *argv[]) std::uint8_t counter = 0; 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)); + if (ptr != nullptr) + { + std::lock_guard lock(ptr->get_mutex()); + ++counter; + std::cout << answer; + answer.clear(); + if (counter == 10) + { + std::cerr << "Cancelling...\n"; + ptr->cancel_stream(); + break; + } + } } pub.join(); std::cout << '\n'; diff --git a/examples/example13_easy_stream.cpp b/examples/example13_easy_stream.cpp index 7c032f1..ca04dda 100644 --- a/examples/example13_easy_stream.cpp +++ b/examples/example13_easy_stream.cpp @@ -11,6 +11,7 @@ #include #include #include +#include // If we are compiling mastodon-cpp, use another include path #ifdef MASTODON_CPP @@ -51,6 +52,16 @@ int main(int argc, char *argv[]) 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 lock(ptr->get_mutex()); + // Parse event stream and clear it afterwards std::vector events = Easy::parse_stream(stream); stream.clear(); @@ -85,7 +96,6 @@ int main(int argc, char *argv[]) cout << "Something undefined happened. 😱\n"; } } - std::this_thread::sleep_for(std::chrono::seconds(1)); } pub_tl.join();