• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android NDK - Multithreading is slowing down rendering

erinys

Lurker
I have an Android app with a C++ library which uses pthreads to break down rendering tasks. This is for devices running Android 4+.


Lets say I have a 100 x 100 array of elements into which I repetitively do CPU-intensive processing. Currently I'm breaking the array up into four 25 x 100 element chunks and handing it off to four Posix threads (from a pool of stalled, pre-created threads). This gives an almost 4x speed increase on iOS and desktop Mac but slower results than single-threading under Android.


So the same code is used successfully to speed up the app on iOS or desktop Mac but in Android it often makes it even slower.
I have done some tests on it and only quite big junks of data speed up when using multi threading. If the whole process (all threads) takes around 2 seconds or more it will speed up in multi threading mode but if it is less (say only takes about 400ms) it will be either the same speed or slower than just calling the rendering function normally. Which could point to thread switching being really slow. The bigger the processing tasks, the more they profit from multithreading. My tasks are usually not as big, but not fast enough in single threading mode.


I have also noticed that on ARM builds the speed difference between slower multi threading and the faster single threading is quite significant (almost twice as fast in multi threading rather than single threading) whereas on x86 builds the multi and single threaded versions will run at about the same speed as single threading on ARM builds. So x86 builds do not get slower on multithreading but also not faster.


Has anyone else had the same behaviour or knows where the slowdown could come from? Are there any special requirements for multithreading on Android? Unfortunately I can't really post any code at the moment but it is all standard posix threading code which works fine on iOS and Mac in general and has been in use for years.
 
My understanding with the Android threading is that if you run concurrent threads simultaneously (the UI and a handler thread for example) then this will share the device's resources, thus slowing it down. This prevented me from using two running threads in my apps.(If I am wrong them please correct me as I can then update my apps accordingly)!
As such, you might want to look into using something like a thread handler to only run when needed for Android, or use Async task if you are not to bothered about the "order" or the results being fed back to the UI thread.
Hope this helps.
 
Back
Top Bottom