SmartFoxServer 3 C++ Client API 3.1.0-beta
Client API for SmartFoxServer 3
Loading...
Searching...
No Matches
sfs3::util::Scheduler Class Reference

Runs functions after a delay, on a dedicated background thread. More...

#include <Scheduler.h>

Public Member Functions

Handle schedule (int64_t delayMs, Task task)
 Schedule a task to run once after the given delay.
bool cancel (Handle id)
 Best-effort cancellation of a not-yet-fired task.

Detailed Description

Runs functions after a delay, on a dedicated background thread.

Owns a single background thread that sleeps until the earliest pending deadline, then runs the corresponding task. Tasks always execute on the scheduler's worker thread, never on the caller's thread. If a task needs to reach the game thread it should push onto the consumer's event queue rather than touch game state directly.

Deadlines use a steady clock so they are immune to wall-clock jumps (NTP corrections, DST). For epoch timestamps see sfs3::util::currentMillis.

The API runs its own instance for internal work, deliberately not exposed: that one is owned by the connection and dies with it, so a task scheduled on it that disposes or replaces the SmartFox would be tearing down the very thread it is running on. Consumers wanting delayed execution should own an instance of this class, whose lifetime they control.

Declaration order matters. Destroying a Scheduler drops the tasks still pending and waits for one already running to return. Declare it after every member its tasks touch, so that it is destroyed before them – reverse that and an in-flight task can reach a member that is already gone.

The class is non-copyable and non-movable.

Member Function Documentation

◆ schedule()

Handle sfs3::util::Scheduler::schedule ( int64_t delayMs,
Task task )
inline

Schedule a task to run once after the given delay.

Parameters
delayMsDelay in milliseconds before the task runs.
taskCallable to invoke on the worker thread.
Returns
A handle that can be passed to cancel().

◆ cancel()

bool sfs3::util::Scheduler::cancel ( Handle id)
inline

Best-effort cancellation of a not-yet-fired task.

Parameters
idHandle returned by schedule().
Returns
true if the task was still pending and is now cancelled, false if it had already fired (or the id is unknown).

The documentation for this class was generated from the following file:
  • util/Scheduler.h