Thread safety and atomicity of logging functions

375 views
Skip to first unread message

Jesse Krebs

unread,
Dec 13, 2019, 11:56:46 PM12/13/19
to android-ndk
I'm using logging functions from the NDK, e.g. __android_log_write():


I haven't been able to find any solid information as to whether these functions are thread safe. I often see them and their non-native counterparts (Log.d(), etc.) used from multiple threads (including, I think, in Google's own code) with no obvious signs of synchronization, which suggests they're expected to be thread safe, but I haven't been able to confirm this.

A related question is whether messages logged via these functions are guaranteed to be atomic with respect to Logcat.

In summary:

- Are the native logging functions (__android_log_write(), etc.) thread safe in the sense of being usable from multiple threads without concern for crashes or other undesirable behavior?

- Beyond that, are messages logged via these functions guaranteed to be atomic (that is, not interleaved at the character level) in Logcat?

J Decker

unread,
Dec 14, 2019, 12:49:52 AM12/14/19
to andro...@googlegroups.com
I'm no official authority.
I can say, there are no issues logging from multiple threads.
The messages are generated as an entire packet and do not interleave.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/2abe8271-c23a-453f-9df3-0b3c5aaebd81%40googlegroups.com.

scgames2

unread,
Dec 16, 2019, 12:28:21 PM12/16/19
to android-ndk
Thank you for your reply. It seems like this would be documented somewhere, but maybe it is and I just haven't found it.

If anyone else knows of any official documentation on this, or perhaps has inside information on it, feel free to share, as I'm trying to gather whatever evidence I can that these functions are thread safe and atomic.

Thanks again for the input - I appreciate it.

On Friday, December 13, 2019 at 11:49:52 PM UTC-6, J Decker wrote:
I'm no official authority.
I can say, there are no issues logging from multiple threads.
The messages are generated as an entire packet and do not interleave.

On Fri, Dec 13, 2019 at 8:56 PM Jesse Krebs <scga...@gmail.com> wrote:
I'm using logging functions from the NDK, e.g. __android_log_write():


I haven't been able to find any solid information as to whether these functions are thread safe. I often see them and their non-native counterparts (Log.d(), etc.) used from multiple threads (including, I think, in Google's own code) with no obvious signs of synchronization, which suggests they're expected to be thread safe, but I haven't been able to confirm this.

A related question is whether messages logged via these functions are guaranteed to be atomic with respect to Logcat.

In summary:

- Are the native logging functions (__android_log_write(), etc.) thread safe in the sense of being usable from multiple threads without concern for crashes or other undesirable behavior?

- Beyond that, are messages logged via these functions guaranteed to be atomic (that is, not interleaved at the character level) in Logcat?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

Glenn Kasten

unread,
Dec 16, 2019, 1:05:11 PM12/16/19
to android-ndk

Shahriar Vaghar

unread,
Dec 16, 2019, 3:21:22 PM12/16/19
to andro...@googlegroups.com
Isn't that a waste of time?
There's no issue here.


To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/f954986e-7487-4c36-850b-4c10e250bdb3%40googlegroups.com.

scgames2

unread,
Dec 16, 2019, 4:08:22 PM12/16/19
to android-ndk
Sorry, I'm not sure to what you're referring.

scgames2

unread,
Dec 16, 2019, 4:08:22 PM12/16/19
to android-ndk
Thanks! I'd looked at the source code before, but it may have been an older version of Android.

I looked at the linked code and managed to trace it through to LogdWrite(), but I'll admit at that point the code is too complex and has too many dependencies for me to analyze easily. And theoretically at least there could still be atomicity or synchronization issues downstream, up to and including Logcat itself. (For example, on iOS, NSLog() is serialized, but using NSLog() from multiple threads simultaneously can still result in messages interleaving at the character level in the Xcode console.)

In any case, empirically I see no stability issues or interleaving in Logcat, and your statement that logging is atomic is reassuring.

Thanks to both of you for taking the time to reply.
Reply all
Reply to author
Forward
0 new messages