I've got a dfhack-internals question for peterix or anyone else who knows.
The sample plugin skeleton.cpp has a comment that dfhack console commands are called from a different thread than the main DF thread.
Is this only true of the console commands? Or do plugin_* run in a different thread?
I assume that vtable hooks must run in DF's main thread, at least.
(I know, I know, I should write test code and figure it out myself. DFHack is dauntingly complex, and I'm trying to get a handle on how it all fits together.)
I guess you already found out yourself, but I'll reply anyway, just in case
There are several threads in DF with DFHack installed. The main simulation thread, the graphics thread, DFHack console thread and DFHack hotkey thread (just waits for hotkey events). A plugin can potentially have code that runs in any and all of them.
Commands need to acquire a lock to work with the game data, which is unlocked every time the game updates (see CoreSuspender and Core::Suspend). The lock has a queue/stack of condition variables that keeps track of all the plugins/threads which requested a suspend. Those are woken up as soon as the simulation thread is in a safe spot (onUpdate()).
Plugins can have callback functions that get called when a particular event happens/is detected. This is called from onUpdate and in turn, from the simulation thread.
So, you could easily have any number of threads and synchronize with the game by using Core::Suspend or the much nicer, CoreSuspender object