Discussion:
duplicated static c++ singleton instances
Rasmus Brandt Olesen
2018-05-03 10:19:15 UTC
Permalink
Hi,

I have a Unity, that uses a c++ library, that is again connected to a java
library.

So the call flow is:
1. Unity call c++ library
2. c++ library calls Java method using JNI
3. java method call same c++ library

In the c++ library we have a static singleton instance.
When we run this on Android 6.0+, then we get the exact same instance of
that singleton, and everything is fine.

But on Android 5.0/5.1 we get 2 different instances

Can anyone help to explain why we see this behaviour on Android 5?

Thanks!,

Rasmus Brandt Olesen
--
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 post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/f29b8b36-7d34-4feb-833a-34931c68a27d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steven Winston
2018-05-18 16:12:10 UTC
Permalink
Fwiw: just incase you are using more than one thread, you might give a double lock a try.
Your code doesn't look like it's doing any locking so you're probably not calling from separate threads but worth mentioning.
--
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 post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/b08ffa85-7a40-4a06-ab62-5c3b6875afc4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steven Winston
2018-05-21 18:20:48 UTC
Permalink
Thanks, this is absolutely correct for compilers that 100% support the C++
standard as 6.7.4 does state:
If control enters the declaration concurrently while the variable is being
initialized, the concurrent execution shall wait for completion of the
initialization.

However, reality isn't always 100% compliant with standards for older
compilers. It's worth mentioning that VS didn't get full support until
2015 (Magic Statics in this listed
table: https://msdn.microsoft.com/en-us/library/hh567368.aspx ). In GCC
the first version to get support was
4.3: https://gcc.gnu.org/projects/cxx-status.html Which means that the
first version of the NDK to receive support would probably have been
Revision 3 from 2010 according to the revision history as that's when gcc
went to 4.4 and every version of clang that's been in the ndk should have
had it.

Given that lollipop is newer, it's not possible that the compiler doesn't
support the feature. And I learned something cool about the language
standard so thanks for the education! :)
It's C++11 already - you don't need double locking. Static instances are
created in a threadsafe manner.
Post by Steven Winston
Fwiw: just incase you are using more than one thread, you might give a double lock a try.
Your code doesn't look like it's doing any locking so you're probably not
calling from separate threads but worth mentioning.
--
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 post to this group, send email to android-***@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/a9874f90-8df8-4729-871b-8610b0d15dcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for 'duplicated static c++ singleton instances' (Questions and Answers)
4
replies
How can I make my application in java run only once?
started 2008-04-09 00:34:11 UTC
programming & design
Loading...