Win32 multithreading: SafeQueue and a 4-thread arena
A week of C++ multithreading with the Win32 API: thread-safe queue, producers and consumers, parallel collision detection, then an SFML arena where four WorkerThreads split the map and talk through messages
C++ · Win32 · Multithreading · SFML · Producer-consumer · Solo
A week of C++ multithreading with the Win32 API at GTech 3: daily exercises, then an arena where four threads split the map.
Context
Week of November 17 to 24, 2025. The course notes (theory, Win32 threads, thread safety) sit in Semaine Exo/Doc/.
Individual work.
What I did
The four exercises of the week, then two versions of the SFML arena: TestMessage (balls that change thread when
they change zone) and TestComplet (players, bonuses, fights and game over). Remote collision damage, zone
transitions and rewards are sent as messages to the SafeQueue of the thread that owns the target entity.
Links
- 5---Multithreading (code on the
Kikibranch)
Weekly exercises (Semaine Exo/)
| Day | Exercise | Content |
|---|---|---|
| D1 | WhackAThread | Console whack-a-mole: each mole is a thread (CreateThread), one thread spawns the moles, events go through a message queue |
| D2-3 | SafeQueue | Bounded ring-buffer queue: CRITICAL_SECTION and two CONDITION_VARIABLEs (not full, not empty), blocking push, tryPush / tryPop, producer-consumer test |
| D3 | CollisionDetector | Collisions between 50,000 spheres: single-threaded, then 2 and 4 threads, a progress monitor thread (Interlocked*) |
| D3-4 | SafeCollision | Same detection as producers and consumers through the SafeQueue, five configurations compared, one with local accumulation of results |
Final arena (Final/TestComplet)
Main thread: SFML rendering (800 x 600), game over, stats
ThreadManager
└── 4 WorkerThreads, each with its entities and a SafeQueue<Message*>
16 ms loop: messages -> update -> collisions -> transitions
Map: 4 x 3 grid of 200 px cells, cell -> thread (index modulo 4)
- 12 players, 8 bonuses (HP, speed, shield, damage). The last player alive wins.
- Local collision (two players on the same thread): damage and bounce computed in place.
- Remote collision: the lower-index thread applies it to its player and sends a
CollisionMessageto the other thread, plus aKillRewardMessageif its player dies. - Transition: a player that changes cell is serialized into a
TransitionMessage, removed from its thread and rebuilt in the target thread. A player's color shows its thread.
Stack
| Tech | Role |
|---|---|
| C++ | Language |
| Win32 API | CreateThread, CRITICAL_SECTION, CONDITION_VARIABLE, Interlocked* |
| SFML 2.6.1 | Arena rendering |
| Visual Studio | Build |