Creating Deterministic Applications Using Asynchronous Timers
- Updated2023-02-21
- 2 minute(s) read
Separate deterministic tasks from non-deterministic tasks and place deterministic tasks in different threads to ensure they receive enough processor resources. You can prioritize the threads to control the amount of processor resources each thread receives.
You can place a task in a thread by writing a callback function that accomplishes a particular task and creating an asynchronous timer to call that function. The asynchronous timer calls the function repeatedly at the period and priority you specify. You can create an asynchronous timer using the functions in the Asynchronous Timer instrument driver. Call NewAsyncTimerWithPriority to create an asynchronous timer with a particular period and priority.
The priority of the asynchronous timer determines the priority of the thread that calls the timer callback. The priority can be any value from -15 to 15. The default priority of a thread in the system is 0. The operating system scheduler runs at priority level 9. Any threads running at a priority level equal to or higher than 9 are not subject to timeslicing by the scheduler and will run until they yield the CPU or are preempted by a higher priority thread.
Cooperatively Yielding Asynchronous Timer Execution
Because of the preemptive scheduling used by the real-time operating system (RTOS), higher priority threads can monopolize processor resources. A high priority asynchronous timer might use all of the processor resources, not allowing lower priority threads to execute.
You must create asynchronous timers with a period large enough to run the callback function and still have idle time before the next iteration to allow lower priority threads to execute. You must do this while maintaining the determinism required by your application. By configuring asynchronous timers in this manner, you can ensure that the timer cooperatively yields processor resources.
![]() |
Note If your application is running on a multiple-CPU system and has a high priority asynchronous timer handling a deterministic task, consider manually assigning the timer to a dedicated CPU. |
