Wheel Timers: The Tick-Tock of Event Scheduling

Wheel Timers: The Tick-Tock of Event Scheduling in Programming


Introduction ๐ŸŒŸ

Welcome to a deep dive into one of the most fascinating data structures out thereโ€”the Wheel Timer! If you’re in the business of efficiently managing timing events, this structure is a must-know. This article will walk you through the basic design, underlying data structure, and the C implementation of Wheel Timers.

๐Ÿ‘‰ GitHub Repository


Basic Design of Wheel Timer ๐ŸŽก

A Wheel Timer is essentially a Linux timer fused with a wheel data structure. Think of it as a wall clock with multiple slots, each representing a unit of time. Though a traditional clock has 60 slots, we’ll use a simplified version with 10 slots for this demonstration.


Linked List in Slots ๐Ÿ—‚๏ธ

Each slot in the Wheel Timer carries a linked list of events. When the “needle” of the timer (analogous to the second hand of a clock) hits a particular slot, all events in the linked list get fired.


Complete Rotations ๐Ÿ”„

The timer maintains a variable ‘R’ to track the number of complete rotations made. This helps in rescheduling and repeated triggering of events.


Event Triggering & Scheduling ๐Ÿš€

Events are essentially function pointers. When the timer’s needle hits a slot, the corresponding events are triggered sequentially. Events can also be set to repeat at intervals, making it highly flexible for a range of applications.


Dynamic Event Scheduling ๐Ÿ—“๏ธ

To schedule an event, you’d calculate the slot where the new event should reside. For instance, if the timer is currently at Slot 2 and you want the event to trigger after 3 seconds, you’d place it in Slot 5.


Rescheduling Events ๐Ÿ”

Events can be rescheduled after they’re triggered. This is done by calculating Current Slot + Time Interval.


C Implementation of Wheel Timer ๐Ÿ› ๏ธ

The core of the Wheel Timer is represented in C as a struct with six crucial members.

typedef struct _wheel_timer_t {
	int current_clock_tic;
	int clock_tic_interval;
	int wheel_size;
	int current_cycle_no;
	_pthread_t *wheel_thread;
	pthread_mutex_t wheel_timer_mutex;
	ll_t *slots[0];
} wheel_timer_t;


Initialization ๐Ÿ—๏ธ

The Wheel Timer is initialized by calling init_wheel_timer, which takes two argumentsโ€”the number of slots and the clock tick interval.

wheel_timer_t* init_wheel_timer(int wheel_size, int clock_tic_interval)


Running the Wheel Timer ๐Ÿƒโ€โ™‚๏ธ

The timer runs on a separate thread, started by the start_wheel_timer function. This function sets up a new POSIX thread to begin executing wheel_fn, the function containing the timer’s core logic.


Why This Matters ๐ŸŽฏ

Understanding the Wheel Timer can offer unparalleled efficiency in tasks that require time-based event management. Itโ€™s commonly used in networking, real-time gaming, and many other time-sensitive applications.


GitHub Repository ๐Ÿ“ฆ

All the code discussed in this blog post, including the C implementation of the Wheel Timer, can be found in the GitHub repository linked below.

๐Ÿ‘‰ https://github.com/ANSANJAY/WheelTimer


Conclusion ๐ŸŽ‰

Wheel Timers are extremely efficient for certain types of problems, especially those that require precise time management. If you’re a developer looking to expand your toolkit, understanding how to implement and use a Wheel Timer can be invaluable.

๐Ÿ‘‰ Interested in More?: If you’re looking to dive deeper, the GitHub repository contains all the source code and additional resources to get you started.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top