PDA

View Full Version : what is better signal or constructor passing parameter to thread performance wise?



toufic.dbouk
12th September 2013, 19:05
Hello team,

i was wondering what is better performance wise, to send a QString through constructor of class sub classing QObject( worker thread ) or through emitting a signal holding the QString as a parameter to the worker thread ?

ChrisW67
12th September 2013, 21:45
If you are able to meet your requirement by passing a string once to the constructor then you would only be setting the string once by slot and the difference is absolutely not worth considering.

toufic.dbouk
12th September 2013, 23:06
Thanks for the reply chrisW67
i kinda knew that but i wanted to make sure of it
yea im just passing 2 String , so ill pass them through the constructor and set them later in the class sub classing QObject as a worker thread.

toufic.dbouk
15th September 2013, 16:05
Hey there,
what about just making a function in the worker Thread sub classing QObject and call it
num1, num2 are inputs from the GUI thread
like ( just a sample code) :
passing num1,num2 through a function in wObject class
wObject is instance of a class sub classing QObject
qThread is an instance of QThread

workerObject *wObject = new workerObject;
QThread *qThread = new QThread;
wObject->setupAtt(num1,num2);
wObject->moveToThread(qThread);
qThread->start();

sample 2:
emiting a signal from main GUI holding num1 , num2 and ctach it in the wObject class

sample 3:
just pass num1 , num2 through the constructor of wObject
instead of signals or passing it through constructor

its not a technical problem, im just asking what is better to do
hope that clarifies it and read the previous posts if its still not clear
Thanks.

amleto
15th September 2013, 16:41
it's not very clear which thread that should be called from and and you don't state how thread-safe that method will be.

anda_skoa
15th September 2013, 22:08
I'd say either pass in constructor or call the setter method before you hand the worker object off to the thread.
Both of those variants don't need any protection against concurrent access since there is only one thread involved at the time.

Cheers,
_

toufic.dbouk
15th September 2013, 23:14
Alright thanks for the clarification.
Best Regards.

amleto
16th September 2013, 00:31
I'd say either pass in constructor or call the setter method before you hand the worker object off to the thread.
Both of those variants don't need any protection against concurrent access since there is only one thread involved at the time.

Cheers,
_

until another programmer comes a along...

Simple setters are not always self-documenting

toufic.dbouk
16th September 2013, 00:41
Simple setters are not always self-documenting

a setter method wouldn't bother a lot though.
mmmm... what would do you recommend then ?
your first answer wasn't clear so i edited the post ( check the previous posts )

wysota
16th September 2013, 06:38
a setter method wouldn't bother a lot though.
mmmm... what would do you recommend then ?

Amleto would suggest a constructor or a setter method with proper thread synchronisation (google if not familiar with the term, it's not Qt specific).

toufic.dbouk
16th September 2013, 11:03
im aware of the term, i usually code in Java and use synchronization in order to ensure that threads accessing the same variable or class execute properly so that one thread doesn't see half of a change done and execute upon it ensuring thread safety, atomic operations, avoiding deadlock, missing an update of value etc...
in other words , so that threads have a mutual exclusive execution or access of a critical section.