Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/countly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "countly/countly_configuration.hpp"

#include <chrono>
#include <condition_variable>
#include <functional>
#include <iterator>
#include <map>
Expand Down Expand Up @@ -349,6 +350,7 @@ class Countly : public cly::CountlyDelegates {
bool enable_automatic_session = false;
bool stop_thread = false;
bool running = false;
std::condition_variable stop_cv; // Wakes updateLoop immediately on stop
size_t wait_milliseconds = COUNTLY_KEEPALIVE_INTERVAL;

size_t max_events = COUNTLY_MAX_EVENTS_DEFAULT;
Expand Down
19 changes: 11 additions & 8 deletions src/countly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ void Countly::_deleteThread() {
mutex->lock();
stop_thread = true;
mutex->unlock();
stop_cv.notify_one();
if (thread && thread->joinable()) {
try {
thread->join();
Expand Down Expand Up @@ -1193,15 +1194,17 @@ void Countly::updateLoop() {
running = true;
mutex->unlock();
while (true) {
mutex->lock();
if (stop_thread) {
stop_thread = false;
mutex->unlock();
break;
{
std::unique_lock<std::mutex> lk(*mutex);
stop_cv.wait_for(lk, std::chrono::milliseconds(wait_milliseconds), [this] {
return stop_thread;
});
if (stop_thread) {
stop_thread = false;
running = false;
return;
}
}
size_t last_wait_milliseconds = wait_milliseconds;
mutex->unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(last_wait_milliseconds));
if (enable_automatic_session == true && configuration->manualSessionControl == false) {
updateSession();
} else if (configuration->manualSessionControl == true) {
Expand Down
Loading