Multi-threading race condition

Started by
4 comments, last by WitchLord 4 years, 10 months ago

In your documentation in the "Thing to think about in multithreading" you state:

  • Multiple threads may execute scripts in separate contexts. The contexts may execute scripts from the same module, but if the module has global variables you need to make sure the scripts perform proper access control so that they do not get corrupted, if multiple threads try to update them simultaneously.

So with that said in the scenario where i have 1 engine created. with 2 threads alive. Now each thread is designated its own personal context-pool. Now note: Each allocate script-class instance assigned to a specific thread and will always run on that thread. aka a script instances executing in thread 0 can not access/talk/write with another script instances executing in thread 1. 

Now the issue comes with this asCThreadLocalData when a context execute a function. When the context executes a function it will access the threadlocaldata and attempt to push the active stack on to the thread shared memory (memory of the thread that allocated the engine, which there is one). Then it follows with a pop when unprepare is called.

As you can see this pushing/popping on the thread local data is not safe ( which makes sense since its called thread-local data, 1-per engine). But that would mean that you cant ever invoke two script classes member functions using 2 different context from 2 different thread simultaneously as it will end up (at some point). Hitting the assert:

as_context.cpp line 173

asASSERT(tld && tld->activeContexts[tld->activeContexts.GetLength() - 1] == ctx); 

Now the fix for me obivously is since each thread is independent (aka any script classess assigned to a thread will always run on that thread) I can just create an engine per-group of thread and then the shared-local memory would be one per-thread. (Since they dont/cant communicate with each other anyways, but that seems bloated).

Im wondering if you can paint me a picture that clarifies the "Multiple threads may execute scripts in separate contexts.". Should i implement my own thread manager? (Looking into this now)

 

Thanks!

Advertisement

After some digging. I got the crash to stop happening. Im dumb. The fix was to build angelscript with AS_NO_THREADS turned off and enabling locking mechanisms. 

 

Though this fixed my issue, still begs the question why locking the pushing and popping to that thread-local data is not needed.

Thread-local data is local to each thread, so it is guaranteed that no two threads access the same memory (unless you mistakenly share the address to the memory across the threads).

That's the reason why it is not necessary to use any locking mechanism when manipulating the thread-local data.

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thank you. Im dumb. Because I didnt build in the project with Multi-threading support ON the thread local data GetLocalData for each context for returning the single main thread-data. Thanks again. 

 

With a follow up question. I know angelscript works with Xbox One + PS4. Does this include multi-threading support for those platform as well. In addition have u tested for the Nitendo-Switch platform for portability.

I'm afraid I cannot say for sure. I personally don't have access to any of these platforms.

However the multithreading support relies on standard C++ libraries so I would be very suprised if it doesn't work on any of them.

The Nintendo Switch uses a 64bit ARM processor and so far AngelScript doesn't have support for native calling conventions on any 64bit ARM platforms. However it should work just fine with AS_MAX_PORTABILITY using the generic calling convention.

 

 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement