Files
phs_v1.0.1.0/commonlibrary/c_utils/docs/en/c_utils_timer.md
2024-09-27 19:16:49 +08:00

2.3 KiB
Raw Blame History

Timer

Overview

Introduction

This is a timer manager. After "Timer" started, users can register several timed events, which can be continuous or once, to it.

#include <timer.h>

OHOS::Utils::Timer

Public Functions

Return Type Name
Timer(const std::string& name, int timeoutMs = 1000)
Construct Timer. If performance-sensitive, change "timeoutMs" larger before Setup. "timeoutMs" default-value(1000ms), performance-estimate: occupy fixed-100us in every default-value(1000ms).
virtual ~Timer()
uint32_t Register(const TimerCallback& callback, uint32_t interval, bool once = false)
Regist timed events.
virtual uint32_t Setup()
Set up "Timer". Do not set up repeatly before shutdown.
virtual void Shutdown(bool useJoin = true)
Shut down "Timer". There are two modes to shut the "Timer" down: blocking and unblocking. Blocking mode will shut "Timer" down until all running events in "Timer" finished. If "timeoutMs" is set as -1, use unblocking mode to avoid deadloop.
void Unregister(uint32_t timerId)
Delete a timed events.

Examples

  1. Examples can be seen in base/test/unittest/common/utils_timer_test.cpp
  2. Running unit test
run -t UT -tp utils -ts UtilsTimerTest

FAQ

  1. Timer should be set up(via Setup()) before use, and shutdown(via Shutdown()) before its deconstruction.

  2. Timer should be set up first and then shutdown. Avoid delegating them to different threads since it may cause multithreading problem.

  3. Set up Timer again would not reset this Timer, but return TIMER_ERR_INVALID_VALUE. If a reset operation is required, shut Timer down first and then set it up.

  4. Parameter in Shutdown() determines whether the thread in Timer would be detach or join. (True(default) --> join; False --> detach). Detach operation would cause possible multithreading problems, thus is not recommended. If a detach operation is required, availability of related objects used in thread_ should be guaranteed.