|
SmartFoxServer 3 C++ Client API 3.1.0-beta
Client API for SmartFoxServer 3
|
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. | |
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.
|
inline |
Schedule a task to run once after the given delay.
| delayMs | Delay in milliseconds before the task runs. |
| task | Callable to invoke on the worker thread. |
|
inline |
Best-effort cancellation of a not-yet-fired task.
| id | Handle returned by schedule(). |