April 4, 2011

reetu reetu
Lab Rat
1 posts

Qt and POSIX

 

I have to develop a Linux daemon that uses the POSIX API to create POSIX threads and POSIX message queues. I would like to use Qt to develop this daemon to take advantage of the Qt libraries and signal/slot mechanism, understanding that the application will not be platform independent. Is it possible to use POSIX API for POSIX threads and message queues with Qt and signals/slots? If so, is there an example of how to do this?

5 replies

April 4, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

These are three questions:

1.) can you use POSIX functions inside Qt? Anser is yes, its C/C++ and you can do anything that C/C++ allows.

2.) Can you use possix threads? Theoretically yes. But, and that is the important thing, you can’t use signal/slot across threads without using QtThreads and Qt message loops.

3.) What do you want to do with POSIX message queues? Inter process communication? Then see answer 1 :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 4, 2011

peppe peppe
Ant Farmer
1026 posts
2.) Can you use possix threads? Theoretically yes. But, and that is the important thing, you can’t use signal/slot across threads without using QtThreads and Qt message loops.

This is slightly incorrect — you can mix QThread and native threads, and even use signals from native threads.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

April 4, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

You can emit Signals from natic threads, but can’t use slots inside them :-)
But I think,. on Linux, QThread objects just encapsulate POSIX threads, don’t they?

But for cross thread signal/slot you need QThreads (at least for the receiving thread) and there a QMessageLoop.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 5, 2011

ZapB ZapB
Robot Herder
1359 posts

Take a look at the QtService solution [qt.gitorious.org]. I have used this successfully in the past.

I would just use QThread for conveniemce since it just wraps POSIX threads.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 5, 2011

unclewerner unclewerner
Ant Farmer
140 posts

Just a note: Local sockets would be more portable than POSIX message queues. Also it scales better if you intend to distribute among more machines later. (see: http://doc.qt.nokia.com/4.7-snapshot/qlocalsocket.html

[EDIT: fixed link, Volker]

 
  ‹‹ Bounding Text Being Rendered Using QPainter?      Qt OpenGL and threads ››

You must log in to post a reply. Not a member yet? Register here!