OSAPIPeriodicTimer Class
The OSAPIPeriodicTimer is a high-resolution timer for periodic thread scheduling. More...
Header: | #include <OSAPI/Timer/Timer.h> |
Inherited By: |
Public Functions
OSAPIPeriodicTimer() | |
virtual | ~OSAPIPeriodicTimer() |
void | Destroy() |
int | SetEvent() |
virtual int | Start(int periodInMs) |
virtual void | Stop() |
int | WaitForEvent() |
Detailed Description
The OSAPIPeriodicTimer is a high-resolution timer for periodic thread scheduling.
The WaitForEvent() method will wait for either a timer event or an event caused by a call to SetEvent().
Note that the timer will not catch up on missed events. This means that if we start the timer with a period set to 100ms, waiting 550 ms before executing WaitForEvent will make the function return immedeatly the first time and then continue its periods.
time ----|---|---|---|---|---|---|---|---|---|---|--- event ----*---*---*---*-----*-*---*---------*-*---*---
Usage
// In some thread's Main() threadTimer.Start(PeriodInMS); // ... while(!Stopped()) { // Wait for timer tick or message event, set timerevent if timer event int event = threadTimer.WaitForEvent() if (event==0) { // Handle timer event } else if (event==1) { // Handle event caused by SetEvent() } else { // New events may be added in the future.. } } threadTimer.Stop(); //
See also OSAPIOneShotTimer, CDPTimer, CDPParameterTimer, CDPTimerMs, CDPParameterTimerMs, CDPTimerCounting, and CDPRampTimer.
Member Function Documentation
OSAPIPeriodicTimer::OSAPIPeriodicTimer()
Constructs an OSAPIPeriodicTimer.
[virtual]
OSAPIPeriodicTimer::~OSAPIPeriodicTimer()
Destructs the timer.
void OSAPIPeriodicTimer::Destroy()
Stops the timer and releases allocated resources.
int OSAPIPeriodicTimer::SetEvent()
Releases WaitForEvent() by signaling the event object.
Returns 0
on failure.
[virtual]
int OSAPIPeriodicTimer::Start(int periodInMs)
Starts the timer.
periodInMs - Timer period in ms.
Returns: Event handle != 0 on success.
[virtual]
void OSAPIPeriodicTimer::Stop()
Stops the timer.
int OSAPIPeriodicTimer::WaitForEvent()
Waits for timer event or event set by SetEvent().
Blocks calling thread until timer event or semaphore event occurs.
Returns 1
if event occured, 0
if timer tick occured. Supposed to return 0
if both event and timer tick occured.
Dependent implementation
- WINDOWS: Uses timer functions SetWaitableTimer() / WaitForMultipleObjects();
- Linux: Uses Mutex and timed condition variable (OSAPIEvent)
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.