Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is passing of data with one signal to multiple slots safe in Qt?

Tags:

qt

My Qt application is continuously receiving data packets from server side and on basis of data, it is constructing object. I have to pass these objects(one at a time) to three different widgets. So, I have connected one signal to slots of all three widgets but I doubt if it is safe to pass objects like that? Suppose the data packets arrive at fast rate, will these get overwritten with new data packet? Can the objects get corrupted due to this or can I loose some packets or data while I am processing the older packet.

Is there a mechanism to ensure that overrun does not happen. Also, will there be some throttling if the data packets at a very fast rate.

like image 817
Gurmeet Singh Avatar asked Jul 24 '12 13:07

Gurmeet Singh


People also ask

Can we connect one signal with multiple slots?

If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted. Signals are automatically generated by the moc and must not be implemented in the . cpp file. They can never have return types (i.e.

Why do we need signals and slots when we can directly call 2 objects using their member functions?

A public slots section contains slots that anyone can connect signals to. This is very useful for component programming: you create objects that know nothing about each other, connect their signals and slots so that information is passed correctly, and, like a model railway, turn it on and leave it running.

How does connect work in Qt?

How Connecting Works. The first thing Qt does when doing a connection is to find out the index of the signal and the slot. Qt will look up in the string tables of the meta object to find the corresponding indexes. Then a QObjectPrivate::Connection object is created and added in the internal linked lists.


3 Answers

Connecting a single signal to multiple slots is a design feature of Qt.

All (almost?) Qt value classes are reference counted, so it's safe to pass them around as much as you like. They are mostly also "copy on write" so you can freely make copies of them where ever you want with no real performance penalty.

Like any other multithreaded code, you need to be careful if multiple threads are writing to the same object at the same time.

like image 184
Martin Beckett Avatar answered Oct 08 '22 21:10

Martin Beckett


Qt's signals and slots are handled in the event loop of the program. When a signal is emitted, calls to any of the connected slots are queued and will be handled during the event loop's tick. Overhead for emitting a signal is quite low; you can emit around 2,000,000 signals a second when connected to a single slot, and 1,200,000 when connected to 2 slots on an i586-500, according to Qt's docs. Handling of the incoming data is likely to be a much bigger part of your program's response time compared to the time spent emitting signals. As long as your data object construction can keep up with the rate of the info coming in from the network, you will be able to emit signals with no problem. The signals will be added to the receiving object's queue to be handled through the next pass of the event loop, so no signals will be lost or overwritten.

If you're worried about multiple receivers modifying the packet data, as long as you are emitting a unique copy of the incoming network data by passing the data object by value to the signal as opposed to a reference or a pointer, you can connect to multiple slots without worrying about your slots modifying the data.

I have used the signals and slots mechanism to handle incoming network data to great effect for a group school project. It uses a UDP protocol to communicate with clients to implement a multiplayer shooter reminiscent of asteroids. I encountered no problems using signals and slots to deliver packet data around the various classes of the server. Most of the problems I did encounter were due to the algorithmic simplicity of the game's netcode. (Everything is updated every packet, no client-side lag compensation, etc) If you're interested, the code's public for viewing here: http://foxx.arksaw.com/svn/tis/ Of interest are the "netlib" "server" and "client" directories.

like image 28
Jonathan Thomas Avatar answered Oct 08 '22 19:10

Jonathan Thomas


I'm not sure with the network programming part of this, but as far as signals and slots go you should be able to connect one signal to as many slots as you need.

like image 1
Rob Avatar answered Oct 08 '22 20:10

Rob