Mixing pthread and Qt
-
We have a project which will require to link together 2 pieces of code :
- the core program, working with Qt (multithreaded with QThread, owning the main thread)
- one multithreaded library (developped internally), based on STL/pthread
At some point we'll have to handle asynchronism between these libraries.
Any point we need to pay good attention to ?
I was especially thinking:
- are signals/slots working from a standard thread (not QThread) ?
- can we create a QObject from any thread ?
- would recursive semaphore work ?
We'll spike a few of these issues, but any feedback or pointer would be greatly appreciated !
-
In general, it's perfectly safe to mix QThreads and native threads (QThread is implemented "on top" of native threads).
bq. # are signals/slots working from a standard thread (not QThread) ?
Yes, but queued connections will require that the receiver is actually living in a QThread with a running event loop.
bq. # can we create a QObject from any thread ?
Yes.
bq. # would recursive semaphore work ?
What does that mean? :)
-
[quote author="peppe" date="1292974148"]bq. # are signals/slots working from a standard thread (not QThread) ?
Yes, but queued connections will require that the receiver is actually living in a QThread with a running event loop.[/quote]
I don't think you necessarily need to have a QThread with running event loop. The QThread has nothing to do with the objects inside them. It is probably perfectly reasonable to implement your own native thread function and start an event loop in it to have queued connections working. However, it would just be reinventing the wheel. -
If you create QObjects inside a non QThread object (like also main thread :-) ) you get a pseudo QThread object which is used for the thread affinity of your object. If you spin an event loop there, signals/slots definitely work there.
-
[quote author="Franzk" date="1293004844"][quote author="peppe" date="1292974148"]bq. # are signals/slots working from a standard thread (not QThread) ?
Yes, but queued connections will require that the receiver is actually living in a QThread with a running event loop.[/quote]
I don't think you necessarily need to have a QThread with running event loop. The QThread has nothing to do with the objects inside them. It is probably perfectly reasonable to implement your own native thread function and start an event loop in it to have queued connections working. However, it would just be reinventing the wheel.[/quote]
Start it how? QEventLoop? All in all it may work, it's just a simple test -- list time I tried, it didn't, so perhaps something has changed in Qt. Even better, I'd say! :-)