I've uploaded the reordering now (along with an improved comment). I'm satisfied that these changes shouldn't introduce any new bugs, and should solve some (rare) race conditions or deadlocks.
Splendid, thank you for this.
Testing reveals a possible problem, although I am not completely sure that it is related to this patch. I have pushed all of your changes to the thread-refactoring branch on my Github repository.
Running fast forwarded with
this (somewhat old) saved game gives a segmentation fault after a few minutes sometimes (but not consistently - perhaps about 2/3rds of the time). I do not get this on the master branch. However, on the master branch, I do get an index out of range fatal error, which I also get when I do not get a segmentation fault on the thread-refactoring branch. A segmentation fault suggests a more serious or fundamental error than an index out of range error, so this may well be a problem with this code, although I am not sure where it arises as attempting to run gdb made my portable computer that I am currently using run out of memory.
It may be worthwhile you having a look at this and seeing whether there is any problem with these updates before I put this out on the master branch.