Skip to content

mitrapinaki/event-loop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

C++ Event Loop (Minimal, Single‑Threaded)

This project implements a clean, minimal, production‑quality event loop in modern C++17.
It demonstrates the core of an event‑driven architecture: a single dedicated thread that executes tasks sequentially, posted from any number of external threads.

This pattern is widely used in high‑performance systems such as trading engines, network reactors, GUI frameworks, and asynchronous I/O libraries.


🚀 Features

  • Single‑threaded event loop
  • Thread‑safe task submission using post()
  • Sequential execution of tasks (no races)
  • Non‑blocking API
  • Minimal and easy to extend
  • C++17 clean implementation

📁 Project Structure

event-loop/ │ ├── include/ │ └── EventLoop.hpp │ ├── src/ │ ├── EventLoop.cpp │ └── main.cpp


🛠 Build Instructions (Clang)

To compile the project manually without CMake:

clang++ -std=c++17 -Iinclude src/EventLoop.cpp src/main.cpp -o eventloop

## Run it 
./eventloop

########################################
What Is an Event Loop?

An event loop is a dedicated thread that repeatedly:

Waits for tasks

Pops them from a queue

Executes them one at a time

This ensures:

No two tasks run concurrently

No shared‑state races

No locks needed inside tasks

Where This Architecture Is Used
Event loops are foundational in many systems:

Networking & Async I/O
Reactor pattern (epoll, kqueue, IOCP)

Boost.Asio strands

libuv (Node.js engine)

Trading & Finance
Market‑data handlers

Order routers

FIX engines

Low‑latency state machines

GUI Frameworks
Qt event loop

Win32 message pump

Cocoa run loop

Game Engines
Main loop for updates, rendering, and events

Embedded Systems
Cooperative multitasking

Deterministic control loops

✅ Benefits
Predictable execution  
Tasks run in strict FIFO order on one thread.

No data races  
All state updates happen on the event loop thread.

No locks needed  
Internal logic stays simple and deterministic.

Low latency  
Avoids context switching and lock contention.

Easy to reason about  
Perfect for state machines and message‑driven systems.

⚠️ Drawbacks
Single‑threaded execution  
CPU‑bound tasks can block the loop.

Not ideal for heavy computation  
Long tasks delay all other tasks.

Requires careful design  
Must avoid blocking calls inside the loop.

Scaling requires sharding  
You may need multiple event loops for multi‑core utilization.

🧩 When Should You Use an Event Loop?
Use it when:

You need deterministic, race‑free state updates

You’re building async I/O or network‑driven systems

You want to avoid locks and shared‑state complexity

You’re implementing a reactor, dispatcher, or state machine

Avoid it when:

You need heavy parallel computation

Tasks may block for long periods

You need to fully utilize all CPU cores without sharding

📄 License
MIT License
Feel free to use, modify, and extend.

🙌 Contributions
Pull requests and improvements are welcome!

Code

About

C++ Event Loop Architecture

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages