Threads on Android

Use of external libraries (DLLs) from ZGE.

Moderator: Moderators

Post Reply
User avatar
Posts: 775
Joined: Wed May 05, 2010 12:16 pm

Threads on Android

Post by Rado1 »

For next generations: several notes on how to implement ZGE external libraries with respect to threads on Android.

First, how threads work in ZGE:

1. JNI_OnLoad function is called from the same thread as ZApplication.OnLoaded section.
2. ZApplication.OnUpdate, AppState.OnUpdate, Model.OnUpdte and ZApplication.OnClose sections are running in the same thread, but different than ZApplication.OnLoaded.
3. All repeated executions of *.OnUpdate sections are executed in the same thread.
4. Each execution of Thread component is running in own thread.

Ville, is it true also for Windows. (I have not tested it yet.)

Consequence of this behavior when using JNI: JNI_OnLoad should not be used to remember JNIEnv, jclass and jmethodID variables and used in consequent calls. They should be either initialized by some call running from OnUpdate section of application, state or model, or in an advanced case, a thread-safe mechanism used to check the current thread (use the gettid() function) should be used; e.g. lazy initialization and updating when running from a different thread than the last call.

The same applies if you are using some C/C++ thread-sensitive library or you are calling external library from threads.

If it is intended to call the external library just synchronously, it does not need to implement some thread-safety mechanisms; even if it is safer to do it so.
Post Reply