Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a native library which I want to use in my Java application. Multiple threads will be using the library as well as multiple Java processes will be using the same library as well. The library is written in C which I am currently compiling with MinGW on Windows. What synchronization mechanics should I use for synchronizing the non thread safe native library?

Thanks in advance!

That would depend a little on the structure of the library. Is it entirely unsafe (ie no two simultaneous threads can call methods) or just not safe for concurrent access to the same data? If so, can the common data be encapsulated inside a Java object, or is it kept static inside the library? Joachim Isaksson Feb 2, 2013 at 8:28 It is a very simple library which only offers some functions, but however must maintain a state. The state consists only of a few variables for each client that calls the library. AlexLiesenfeld Feb 2, 2013 at 8:31 @Joachim Isaksson: Concurrent access is not thread safe. Clients pass information to the library which the library stores for a client usually - it keeps track of the actions a client have done. So in principle there is not much race condition possibilities, but however, I simply wonder if using pthread-mutexes will be sufficient to create a new entry in the hashtable where I keep track of client actions. AlexLiesenfeld Feb 2, 2013 at 8:44 How does this help in the case where multiple Java processes are loading the same shared library? Java42 Feb 17, 2013 at 17:08 @Java42 Different process will have entirely separate VMs, so the JNI thread-safety issues (which, AFAIK, are due to shared access to JVM resources) shouldn't apply. In fact, I don't think shared libraries ever become problematic just because they're loaded by multiple processes (e.g. almost every process loads some of the system-call libraries on modern OSes), unless the shared libraries are unsafely accessing system resources. Kyle Strand Jan 22, 2016 at 17:04

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .