A simple multithreaded order matching engine built in C++17 to understand how stock exchanges process buy and sell orders. The project simulates a basic exchange by continuously generating orders, storing them in an order book, matching compatible orders, and executing trades using a producer-consumer model.
This project was built mainly to learn modern C++, multithreading, STL containers, synchronization techniques, and the basics of exchange order matching systems.
- Buy and sell limit orders
- Price-time priority based matching
- Partial and complete order execution
- Thread-safe producer-consumer queue
- Concurrent order processing using multiple threads
- Live order book updates
- Trade execution records
- Basic engine statistics
- C++17
- STL
- std::thread
- std::mutex
- std::condition_variable
- std::priority_queue
- GCC (MSYS2)
OrderMatchingEngine/
│
├── include/
│ ├── Order.h
│ ├── Trade.h
│ ├── ThreadSafeQueue.h
│ └── MatchingEngine.h
│
├── src/
│ ├── main.cpp
│ ├── Order.cpp
│ ├── Trade.cpp
│ ├── ThreadSafeQueue.cpp
│ └── MatchingEngine.cpp
│
├── docs/
├── README.md
├── CMakeLists.txt
└── .gitignore
The project uses two separate threads.
- The Producer Thread generates random buy and sell orders.
- The Consumer Thread continuously removes orders from a thread-safe queue and sends them to the matching engine.
The matching engine keeps separate buy and sell order books. Whenever a new order arrives, it checks whether an opposite order exists that satisfies the price conditions. If a match is found, a trade is executed. Orders are either completely filled or partially filled depending on the available quantity.
Orders with better prices get higher priority, and if two orders have the same price, the one that arrived earlier is processed first.
The simulator is divided into four main components:
-
Producer Thread
Generates random buy and sell limit orders. -
ThreadSafeQueue
Stores incoming orders and synchronizes communication between producer and consumer threads using mutexes and condition variables. -
Matching Engine
Processes incoming orders, maintains buy and sell order books, and executes trades whenever matching conditions are satisfied. -
Order Book
Maintains pending buy and sell orders using price-time priority.
During this project I learned and used:
- Object-Oriented Programming
- STL containers
- Priority Queues
- Producer-Consumer pattern
- Thread synchronization
- Mutex locking
- Condition Variables
- Basic low-latency programming concepts
- Order matching algorithms
- Concurrent programming
Compile using GCC:
g++ -std=c++17 -Iinclude src/*.cpp -o OrderMatchingEngine.exeRun the executable:
./OrderMatchingEngine.exeor on Windows PowerShell
.\OrderMatchingEngine.exeIncoming BUY Order
Incoming SELL Order
Trade Executed
Price: 101
Quantity: 20
Remaining Quantity: 10
Current Order Book
Engine Statistics
Some improvements that can be added in the future are:
- Market orders
- Order cancellation
- Order modification
- Multiple producer and consumer threads
- Performance benchmarking
- CSV input/output
- Logging to files
- Unit testing
- GUI or web dashboard
Before starting this project, I had very little experience with multithreading in C++. Building this simulator helped me understand how different threads communicate safely, how synchronization primitives work, and how matching logic is implemented using STL containers. It also gave me practical experience in organizing a C++ project into multiple source and header files instead of writing everything in a single file.
Although this is a simplified simulator, it helped me understand the core ideas behind exchange order matching systems and concurrent application design.
| Operation | Complexity |
|---|---|
| Insert Order | O(log n) |
| Remove Best Order | O(log n) |
| Peek Best Order | O(1) |
| Match Orders | O(log n) (per match) |
The project uses priority queues for maintaining buy and sell orders efficiently while preserving price-time priority.