Skip to content

Use condition_variable for interruptible thread sleep in updateLoop#147

Open
jslauthor wants to merge 1 commit intoCountly:masterfrom
hypernuclear:fix/interruptible-stop
Open

Use condition_variable for interruptible thread sleep in updateLoop#147
jslauthor wants to merge 1 commit intoCountly:masterfrom
hypernuclear:fix/interruptible-stop

Conversation

@jslauthor
Copy link

Summary

  • Replace std::this_thread::sleep_for() in updateLoop() with std::condition_variable::wait_for()
  • Add stop_cv.notify_one() in _deleteThread() to wake the sleeping thread immediately

This makes stop() return instantly instead of blocking for up to the full update interval (e.g. 30 seconds). Currently, _deleteThread() sets stop_thread = true and calls thread->join(), but the thread is stuck inside an uninterruptible sleep_for() and won't check the flag until the sleep finishes.

This causes GUI applications to hang for up to 30 seconds on quit when the SDK's static singleton destructor calls stop().

Changes

include/countly.hpp

  • Added #include <condition_variable>
  • Added std::condition_variable stop_cv member

src/countly.cpp

  • updateLoop(): Replaced sleep_for with stop_cv.wait_for() using stop_thread as predicate
  • _deleteThread(): Added stop_cv.notify_one() after setting stop_thread = true

Test plan

  • Verified stop() returns instantly instead of blocking
  • Verified update interval behavior is unchanged during normal operation (same sleep duration unless notified)
  • No changes to public API

Replace std::this_thread::sleep_for() in updateLoop() with
std::condition_variable::wait_for() so that _deleteThread() can
wake the background thread immediately via notify_one().

This makes stop() return instantly instead of blocking for up to
the full update interval (e.g. 30 seconds in production builds).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jslauthor
Copy link
Author

Sorry, accidentally created this. Will open again if needed!

@jslauthor jslauthor closed this Mar 17, 2026
@jslauthor jslauthor reopened this Mar 17, 2026
@jslauthor
Copy link
Author

Okay, I do need it. 😅 This lets us cancel any pending requests so the app can close cleanly. Let me know if you'd like any changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant