fix(Thread): fix Thread reuse error, add thread interrupt feature (#4942)

* enh(Thread): add thread interrupt feature

* fix(Thread): fix Thread reuse error
This commit is contained in:
siren186
2025-09-11 15:35:23 +08:00
committed by GitHub
parent e6f661d313
commit a31dbe6842
8 changed files with 162 additions and 2 deletions

View File

@@ -256,6 +256,35 @@ public:
/// Negative value means the thread has
/// no CPU core affinity.
bool isInterrupted();
/// Tests whether current thread has been interrupted.
/// Return true if the task running on this thread should be stopped.
/// An interruption can be requested by interrupt().
///
/// This function can be used to make long running tasks cleanly interruptible.
/// Never checking or acting on the value returned by this function is safe,
/// however it is advisable do so regularly in long running functions.
/// Take care not to call it too often, to keep the overhead low.
///
/// See also checkInterrupted().
void checkInterrupted();
/// Tests whether current thread has been interrupted.
/// Throws Poco::ThreadInterruptedException if isInterrupted() return true.
///
/// Note: The interrupted status of the thread is cleared by this method.
void interrupt();
/// Interrupts this thread.
///
/// This function does not stop any event loop running on the thread and
/// does not terminate it in any way.
///
/// See also isInterrupted().
void clearInterrupt();
/// Clear the the interrupted status.
protected:
ThreadLocalStorage& tls();
/// Returns a reference to the thread's local storage.
@@ -301,6 +330,7 @@ private:
int _id;
ThreadLocalStorage* _pTLS;
Event _event;
std::atomic_bool _interruptionRequested;
friend class ThreadLocalStorage;
friend class PooledThread;