Multithreading troubles

Hi guys,

A single-threaded Windows app using OCAS is working fine. After making it multi threaded I encounter an error that at some random runtime point when I call a method from the object using its handle (handle->object method) it tries to run a method from the handle object itself (like handle.handle method) and, of course, I'm receiving GPF. It happens only in multi-threading environment. MMGT_OPT=1, MMGT_REENTRANT=1.

Do someone have any thoughts of what is going on?

Francois Lauzon's picture

Hello Kostya,
It's because the Handle in OCC any not thread safe, you have to protect your handle operation using mutex or something similar. We have encoutered this problem with earlier version of OCC, and as of version 6.1.1, it's still not thread safe. The problem is to protect the reference count, using mutex to do that for each handle is overkill, but using atomic increment operation, like done in Trolltech Qt, is quite simple and efficient (have a look at the class QSharedDataPointer). So we ended up developping our own smart pointer for class that used to be Handle based and it's been working fine for a while. We still have to protect the OCC library if it's going to be used concurently.

Good Luck,
Francois.

Kostya Mashukov's picture

François, thank you. May be it's the reason.
Actually, it is somewhat strange to have an thread-safe memory manager and completely thread-unsafe handle mechanism...:)