THIS PROJECT WAS THE WORK OF 3 PEOPLE BUT I COULDN'T FORK THE ORIGINAL REPO DUE TO MY SCHOOL'S ORGANIZATION
- [Kikii95] (https://github.com/Kikii95)
- [Skaro971] (https://github.com/Skaro971)
This READMe has been made by Kikii95 on the original repo, I only modified a few info and the display !
Multithreading Battle Royale is a C++ project simulating thousands of moving entities in real-time using multithreading and a spatial partitioning system. Each area of the arena is managed by a dedicated thread, and entities interact via collisions and damage.
Technologies used:
- C++20
- SFML 3.0.2
- Windows API (native threading)
- CMake for build management
The project demonstrates multithreaded entity simulation, real-time collision handling, and message-based inter-thread communication.
- Visual Studio 2022 (or compatible)
- CMake >= 3.20
- Windows SDK
- Clone the repository:
git clone https://github.com/tiagzoc/MultiThreading.git
- Create a build directory:
Note: you can name the directory however you want.
mkdir build
- Open a cmd and write this command:
Note: you need to modify the
buildparameter with your build directory's name (if needed).
cmake -S . -B build
- Build the project:
cmake --build . --config Debug
- Run the
MultiThreadingexecutable.
Note: if you're not using Visual Studio, you will probably need to set
MultiThreadingproject as aStartup project.
- The arena is divided into zones, each handled by a dedicated worker thread.
- Entities (balls) move, collide, inflict damage, and are removed when health reaches zero.
- The last surviving entity wins the match.
- Entities crossing zone boundaries are transferred between threads via asynchronous messages.
- GameManager::Init() initializes the grid and spawns balls.
- ThreadManager::StartAll() starts the worker threads.
- Each frame:
- Workers process messages → update → detect collisions → handle transitions.
- WaitForMultipleObjects() ensures all threads have finished.
- Renderer::Render() draws the current state.
- Shutdown() cleanly stops all threads.
| Primitive | Usage |
|---|---|
| CreateThread | Worker thread creation |
| SetEvent / WaitForSingleObject | Frame start/end signals |
| WaitForMultipleObjects | Wait for all threads |
| SRWLOCK | Read/write protection for entities |
| SafeQueue | Thread-safe message queue |
Two-phase detection:
- Local phase: collisions between entities in the same thread (O(n²) local)
- Cross-thread phase: entities near borders check neighboring zones
The thread with the lowest ID handles cross-thread collisions to avoid duplicates; the other receives a CollisionMessage.
- Dynamic spatial partitioning (NxM threads)
- Entity transfer between threads via messages
- Damage and elimination system
- HP bonuses on kills
- Player names displayed when few entities remain
- End-of-game screenshots
- Victory screen with the winner
- Performance measurement per frame (Loop/Threads/Update/Render in ms)
Enable in main.cpp:
GameManager::Instance()->Init(width, height, balls, displayStat, displayCollision, displayPerf);
Example output (displayPerf = true): Loop: 16ms | Threads: 8ms | Update: 1ms | Render: 5ms
| Message | Data | Usage |
|---|---|---|
| TransitionMessage | Position, stats, source/destination thread | Transfer entity between threads |
| CollisionMessage | Target ID, damage, attacker position | Cross-thread collision |
| KillRewardMessage | Killer ID, bonus HP | Reward for a kill |
- Implementing multithreaded entity simulation with dynamic zone partitioning.
- Using thread-safe queues and Windows threading primitives.
- Measuring per-frame performance and debugging multithreaded behavior.
- Designing message-based inter-thread communication.
- GPU-based rendering for better performance with thousands of entities.
- Dynamic bonus spawn on the map as the game progress.
- Configurable arena shapes and sizes.
- More advanced collision optimization (spatial hashing, quadtrees).