[Qt-interest] Data exchange in threads

Nicholas Shatokhin n.shatokhin at gmail.com
Fri Apr 29 00:44:45 CEST 2011


So, how I did this.

When server create new thread, he connect thread's signal  
emitMessage(threadId, message) to server's slot receiveMessage(threadId,  
message) and server's signal emitMessage(threadId, message) to thread's  
slot receiveMessage(threadId, message).
So, when thread need to send message to another thread, he emit signal  
emitMessage with sending message and receiver's id to server. Server catch  
signal and emit signal to all threads. All threads compare their ids with  
threadId from signal and if its matching do something with this message.  
It's works, but I have doubts about performance of this method. I hope my  
system will be used by millions people and maybe exists more comfortable  
and performance method?

Best regards,
Nick.

Tue, 26 Apr 2011 16:54:49 +0300 було написано BRM <bm_witness at yahoo.com>:

> There are numerous ways, which is best depends on what you are doing.
>
> 1. Use a modified signal/slot:
> - include a 'client ID' in the signal
> - the network connection is wrapped such that it knows its 'client ID'
> - only forwards data to the client if the client ID matches.
>
> 2. Use dedicated objects with signals/slots
> - each object interfaces for the entire session of the client,  
> interfacing to
> the internal parts as necessary, not necessarily with signals/slots.  
> This will
> require locks/etc.
> - each object has a normal signal/slot interface to the client it is  
> servicing.
>
> 3. QObject->sender()
> - when you receive a call to a certain slot, get the signal sender,  
> generate the
> result and then send it back only to the sender.
> - this requires that the sender() be able to be qobject_cast<>() to a  
> known
> QObject derived class with a known interface (slot) to send the data  
> back on.
>
> I have used #3 for certain tasks. #2 may be more to your fitting, but it
> certainly the harder method in most cases since you have to figure out  
> the
> locks, etc and you can't take full advantage of signals/slots.
>
> So as I said - it's a matter of your system design and what works best.  
> For me,
> #3 was a simple enough solution for something where I wanted one client  
> to have
> total control of a multi-message process but it's not necessarily  
> correct for
> you - and it certainly breaks signal-slot OO practices (since you're not  
> really
> suppose to care who calls a slot).
>
> $0.02
>
> Ben
>
>
>
> ----- Original Message ----
>> From: Николай Шатохин <n.shatokhin at gmail.com>
>> To: "qt-interest at qt.nokia.com" <Qt-interest at qt.nokia.com>
>> Sent: Tue, April 26, 2011 9:02:48 AM
>> Subject: [Qt-interest] Data exchange in threads
>>
>> Hello.
>>
>> I have one server on qt4, one client on qt4 and entry point on  php.
>>
>> Client can send data to php by json and send data to server  (this
>> action dynamically create new thread on server). Php can send data  on
>> server too (it's no difference for server who send data). So we  have
>> two thread created dynamically - one for client, one for entry  (this
>> threads on server can be thousands or millions). So, how can  entry's
>> thread send data only for this client's thread? If I'll  you
>> signals/slot, all threads send data to all threads and all  threads
>> must do checking (it's signal for me or not?). It's very slow. So,  can
>> I use some quick  method?
>> _______________________________________________
>> Qt-interest  mailing list
>> Qt-interest at qt.nokia.com
>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>


-- 
За використання революційного клієнта електронної пошти Opera:  
http://www.opera.com/mail/



More information about the Qt-interest-old mailing list