views:

41

answers:

1

I'm working on a little project that uses DirectShow/COM for capture, and DShow makes callbacks using its own threads when my application gets imaging data.

I'm also using Qt in my project, and I'm wanting to use Qt for synchronisation and thread-safety. I'm wondering how I can use Qt Threads in this case.

I understand I can also use Win32's CriticalSection functions, but this would make it harder to port my code to other platforms (as the DShow stuff is the only Windows-specific code in my project).

My question is: "how do I use Qt's thread safety features when working with non-Qt threads?"

+2  A: 

(Disclaimer: Haven't used QT Threads myself. -- But I crosschecked the sources on my QtCreator here.)

From looking at http://doc.qt.nokia.com/4.6/threads.html#the-threading-classes and from my experience with other threading libs I would say that you probably can safely use all the threading constructs (QMutex, QMutexLocker, ... but also QFuture, ...) that do not depend on the existence of a QThread object for the thread they "operate on". That is, safely use from one of your COM threads.

On the how: The QMutex class will be used exactly the same, no matter if you launched a thread via QThread or if it's a COM thread. That's because it would have to use the underlying OS threading support directly and doesn't really need a thread created via a QThread object.

Since you mention critical sections: Qt "only" seems to implement a generic QMutex class. Regarding functionality I guess this is pretty much allright. In a heavily multithreaded App it may (may!) pay off to use win32 CriticalSections directly (via a thin wrapper with a QMutex like interface) for performance reasons.

Martin