Hi, everyone.
I am continuing to work through the archive while implementing a version of HH for Mac OS X using SFML and LLVM (Clang). I've had a lot of fun recently trying to port changes in days 122-125 (multi-threading). It'd took a bit of reading but I've managed to make my own version.
There is still one issue that I can't figure out. When I compile my code with -O2 optimisation level, everything works OK (or, at least, looks and behaves the same as in Casey's videos). Threads aren't conflicting and processing strings properly, rendering isn't broken. However, when I compile with -O0, everything goes nuts. Here's a short gif that showcases the problem:
https://gyazo.com/adcef74e2366f4af5959ea0e5b2a5106
This stuff just happens but itself, I am not pressing anything on the keyboard.
I've played around with the code, trying to comment out thread-related stuff and, apparently, weird things start to happen when I add the following line, two hero avatars appear and start shooting swords:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | struct work_queue_entry_storage
{
void *UserPointer;
};
struct work_queue
{
uint32 volatile EntryCompletionCount;
uint32 volatile NextEntryToDo;
uint32 volatile EntryCount;
work_queue_entry_storage Entries[256];
};
...
work_queue Queue = {};
|
If I comment out the "void *UserPointer;" part, everything's OK. When the whole code related to threads is present, the game behaves as in the gif above.
Here's a complete snippet of thread-related code from my code base:
https://gist.github.com/roman-d/8bcf27c41353d4db3eb91ee1ce2957af. I would appreciate some pointers on what could be happening. How does this mess up the behaviour of the program in such strange way?