目录

timer.h

Overview

Declares the C APIs of the timer. Provides timer capabilities based on QoS levels, supports executing a callback function after a specified timeout, and can be used for scenarios such as delayed task scheduling.

File to include: <ffrt/timer.h>

Library: libffrt.z.so

System capability: SystemCapability.Resourceschedule.Ffrt.Core

Since: 12

Related module: FFRT

Summary

[h2]Function

Name Description
FFRT_C_API ffrt_timer_t ffrt_timer_start(ffrt_qos_t qos, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat) Starts a timer on the FFRT worker thread. Avoid calling exit or ffrt_timer_stop in cb to prevent undefined behavior or deadlock.
FFRT_C_API int ffrt_timer_stop(ffrt_qos_t qos, ffrt_timer_t handle) Stops a timer on the FFRT worker thread. This API is a blocking API. Avoid calling this API in the callback function to prevent deadlock or synchronization issues. When the callback corresponding to handle is being executed, this function waits for the callback to complete before returning.

Function Description

[h2]ffrt_timer_start()

FFRT_C_API ffrt_timer_t ffrt_timer_start(ffrt_qos_t qos, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat)

Description

Starts a timer on the FFRT worker thread. Avoid calling exit or ffrt_timer_stop in cb to prevent undefined behavior or deadlock.

Since: 12

Parameters

Name Description
ffrt_qos_t qos QoS level of the worker thread that runs the timer.
uint64_t timeout Timeout duration, in milliseconds.
void* data Pointer to the user data passed to cb.
ffrt_timer_cb cb User callback function executed after the timeout.
bool repeat Whether to repeat the timer. true indicates repeat, and false indicates execution only once.

Returns

Type Description
FFRT_C_API ffrt_timer_t Timer handle. If the callback function is a null pointer or the QoS mapping is not registered, -1 is returned.

Reference

ffrt_timer_stop

[h2]ffrt_timer_stop()

FFRT_C_API int ffrt_timer_stop(ffrt_qos_t qos, ffrt_timer_t handle)

Description

Stops a timer on the FFRT worker thread. This API is a blocking API. Avoid calling this API in the callback function to prevent deadlock or synchronization issues. When the callback corresponding to handle is being executed, this function waits for the callback to complete before returning.

Since: 12

Parameters

Name Description
ffrt_qos_t qos QoS level of the worker thread that runs the timer. It must be consistent with the QoS level used in ffrt_timer_start.
ffrt_timer_t handle Handle of the target timer, returned by ffrt_timer_start.

Returns

Type Description
FFRT_C_API int

Returns 0 if the operation is successful;

otherwise, returns -1.

Reference

ffrt_timer_start