I have a cache implementation in C++ that is accessed from JAVA code via JNI. To allow concurrent access to the cache I need to lock it and I am confused what type of lock should I use in my C++ program? should I use JNI's montiorEnter/Exit as the threads that execute the jni code are java threads or std::mutex cacheMtx? Thanks
Asked
Active
Viewed 203 times
0
-
Do as much in Java as possible. If there is a corresponding Java object for your C++ cache, just make the native methods that access the cache `synchronized`. See https://stackoverflow.com/questions/16848282/native-method-synchronization. If you have many reads and few writes, make the native methods `private`, then implement public data access methods and use something like a `ReentrantReadWriteLock` internally to your object. – Andrew Henle Nov 18 '17 at 12:23
1 Answers
0
Take a look here for a sample code where JVM is accessed from different threads.
http://jnicookbook.owsiak.org/recipe-no-027/
In general, you want to make sure to gain access to JVM, use it, and then, release everything.
In your case, you migh have slightly different approach, as you probably won't call JVM back, but still, you can utilize pthread_mutex as you are inside the same shared library loaded from your JVM.

Oo.oO
- 12,464
- 3
- 23
- 45