Skip to content
Nick Renieris edited this page Sep 5, 2016 · 3 revisions

I wanted to make a quick note here that is worthy of it's own page and that is: Thread Safety.

Multiple things can happen if you're not careful when using Threads and I want to name the important ones that pertain to NMSE. Some of the things mentioned will be taught in the Advanced Tutorials but it is important that you don't do these things as it can lead to unexpected results (most of the time it will seem fine but you're introducing an error that can occur randomly).

Writing Hooks In A Thread

So using hooks while in a thread and all that is perfectly fine. NMSE is injected via a RemoteThread and so you should be able to execute most function calls without causing any clashes (though some things write to global data which could cause a problem). But WRITING process memory inside of your own thread (whether to create a hook or what not) can cause (and does most of the time) a crash.

The crash happens because you are overwriting functions that are potentially being called and thus the program will crash as you're writing to it just as another program is accessing it (this is the definition of not being thread safe, not literally but you get the gist).

Be Careful When Using Some Globals

Some globals such as global_Memory and local_Memory (it's a global it's just not called that) are used in methods statically (they are externed in the header so they are always defined) and are then by definition NOT thread safe.

So whenever you are using something like those you need to avoid calling them outside of the loading function (OnStart) since other mods could be using it. This goes in hand with the first point as when you create a thread and return, NMSE than continues to load in the next mod in the iterator thus giving the possibility that both mods attempt to write into the same data at the same time.