0

In my c++ program, I use a proprietarry library (.dll) which is not thread-safe. :-(

In this library there is a specific scientific computing.

Is there a way to safety start several computing of this library in parallel with threads ? (1 process, many threads)

My program "is like" a "for" loop on which call each time the computing of my not thread-safe library

scanpat
  • 309
  • 1
  • 4
  • 8
  • 2
    Yes: synchronise everything yourself. – Kerrek SB Jan 09 '13 at 16:07
  • Most libraries nowadays are thread safe in the sense that you can run multiple instances at the same time. For example: most implementations of the STL. Have a look on this discussion about it http://www.sgi.com/tech/stl/thread_safety.html. However, it is hard to know without looking at the actual code. – Ben Jan 09 '13 at 16:13
  • Avoid deadlock by design. – David Ruan Jan 09 '13 at 16:13
  • 1
    @scanpat: "synchronize everything yourself" means have a big old mutex which you hold whenever using the library. It's probably not really what you want when you asked for "several computing of this library in parallel with threads". Only one of your threads will hold the mutex at any one time, so it's not really parallel use of the library. If the "library itself" is thread-safe but there are individual classes/structures within it that are unsafe, then you might synchronize those structures. But I assume by "the library is not thread-safe" you mean that it has unsafe global state. – Steve Jessop Jan 09 '13 at 17:53
  • Synchronizing will guarantee the correct execution, but it doesn't bring any parallelism the OP wants to see. – minjang Jan 10 '13 at 19:14

2 Answers2

2

Sounds like you want to load the DLL multiple times. Take a look at Load the same dll multiple times.

Community
  • 1
  • 1
mkluwe
  • 3,823
  • 2
  • 28
  • 45
  • I have not done that by myself, but if you follow the "copy the DLL to a temporary file" advice, that should not be too hard. – mkluwe Jan 09 '13 at 16:51
  • Yeah, that's what I mean with "not trivial". Copying files around is... meh. But unluckily there's no other way, since loading a DLL twice otherwise will only increment the reference count. I wonder if it works if you just create a junction, though ... technically that _is_ a different name, and it needs not copy anything. – Damon Jan 09 '13 at 16:53
1

A very simple approach would be forking multiple slave processes in your for loop. The slave process loads the non-thread-safe module and does computations, and finally returns the result to the parent process via either a simple return code (if the result fits into 4 bytes), IPC, or file.

Of course, this approach assumes that the parallel computations do not need any interaction with the others.

minjang
  • 8,860
  • 9
  • 42
  • 61