what is better signal or constructor passing parameter to thread performance wise?
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 ?
Re: what is better signal or constructor passing parameter to thread performance wise
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.
Re: what is better signal or constructor passing parameter to thread performance wise
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.
Re: what is better signal or constructor passing parameter to thread performance wise
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
Code:
workerObject *wObject = new workerObject;
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.
Re: what is better signal or constructor passing parameter to thread performance wise
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.
Re: what is better signal or constructor passing parameter to thread performance wise
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,
_
Re: what is better signal or constructor passing parameter to thread performance wise
Alright thanks for the clarification.
Best Regards.
Re: what is better signal or constructor passing parameter to thread performance wise
Quote:
Originally Posted by
anda_skoa
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
Re: what is better signal or constructor passing parameter to thread performance wise
Quote:
Originally Posted by
amleto
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 )
Re: what is better signal or constructor passing parameter to thread performance wise
Quote:
Originally Posted by
toufic.dbouk
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).
Re: what is better signal or constructor passing parameter to thread performance wise
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.