Skip to content

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. Made by 3 people in 2 weeks.

Notifications You must be signed in to change notification settings

tiagzoc/MultiThreading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multithreading Battle Royale

THIS PROJECT WAS THE WORK OF 3 PEOPLE BUT I COULDN'T FORK THE ORIGINAL REPO DUE TO MY SCHOOL'S ORGANIZATION

OTHER CONTRIBUTORS :

This READMe has been made by Kikii95 on the original repo, I only modified a few info and the display !

General Information

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.


How to Build and Run

Prerequisites

  • Visual Studio 2022 (or compatible)
  • CMake >= 3.20
  • Windows SDK

Steps

  1. Clone the repository:
git clone https://github.com/tiagzoc/MultiThreading.git
  1. Create a build directory:

Note: you can name the directory however you want.

mkdir build
  1. Open a cmd and write this command:

Note: you need to modify the build parameter with your build directory's name (if needed).

cmake -S . -B build
  1. Build the project:
cmake --build . --config Debug
  1. Run the MultiThreading executable.

Note: if you're not using Visual Studio, you will probably need to set MultiThreading project as a Startup project.


How the Project Works

Core Concepts

  • 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.

Main Flow

  1. GameManager::Init() initializes the grid and spawns balls.
  2. ThreadManager::StartAll() starts the worker threads.
  3. Each frame:
    • Workers process messages → update → detect collisions → handle transitions.
    • WaitForMultipleObjects() ensures all threads have finished.
    • Renderer::Render() draws the current state.
  4. Shutdown() cleanly stops all threads.

Synchronization

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

Collision System

Two-phase detection:

  1. Local phase: collisions between entities in the same thread (O(n²) local)
  2. 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.


Features

  • 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)

Performance Metrics

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 System

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

What I Learned

  • 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.

Possible Improvements

  • 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).

Demo

Gameplay Video

About

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. Made by 3 people in 2 weeks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published