if I want to inform one thread from another thread how should I do it?
Can I call the run function of the thread that I want to call from the thread that the process is being run in it?
![]()
if I want to inform one thread from another thread how should I do it?
Can I call the run function of the thread that I want to call from the thread that the process is being run in it?
![]()
run() method of QThread will automatically called whenever "start()" method of QThread called.
Qt Code:
void Dialog::on_pushButton_Start_clicked() { //Started if(rThread->Stop == true) rThread->Stop = false; //My_Initializer(); if(rcvThread->Stop == true) rcvThread->Stop = false; // data_child should passed before starting of thread rcvThread->setChildData(data_child); // rcvThread->run(data_child); if(prcsThread->Stop == true) prcsThread->Stop = false; // data_child should passed before starting of thread prcsThread->setChildData(data_child); // prcsThread->run(data_child); }To copy to clipboard, switch view to plain text mode
Hope this will help you
Dear buddy,
Thanks for your response.
I have already 5 thread which consist the main thread (or the ui thread).
I want all threads that I have work concurrently and continously giving data from ui and do their tasks without stop until I push the stop button.
Also I have a common data class between them and I need that each thread inform other when it do its duty and its own processes on this common data!
one the code is so long and the site don't let me to upload it or copy it in the response.
But I really dont know how should I manage them.
:confused:
regards
Last edited by havij000; 13th August 2013 at 07:27.
Dear karankumar1609's,
thanks for your reply.
would you please put the complete code here, I really do not understand what is theis what should it do!prcsThread->setChildData(data_child);
thanks
regards,
Both of your threads are missing the implementation of QThread::run()
You probably want to call your run functions from it.
Cheers,
_
Dear buddy,
thanks for your response.
would you please explain it more about it?Both of your threads are missing the implementation of QThread::run()
Isn't it enough to implement the run function?
Qt Code:
void Recive_Thread::run(Data_Class *data_rcv_child)To copy to clipboard, switch view to plain text mode![]()
Yes, implementing the run function is enough. But as I said, you are not implementing the run function. Or rather you are implementing another function called run that does not match the override requirements for QThread::run().
Look at QThread's documentation. Its run method looks like this
Now look at your run function
Don't they look different?Qt Code:
void run(Data_Class *data_rcv_child);To copy to clipboard, switch view to plain text mode
"Blimey", you say, "indeed, one of the has a function argument!"
So if you want to execute run(Data_Class*) in a thread, what do you do?
Easy, right?
You add run() to your class and let it calls run(Data_Class*)
Cheers,
_
yes I got it.
you mean that I should emplement the run function:
and define another function to get and set the values that I need to set!?
Regards,
It is just the replacement of your codeprcsThread->setChildData(data_child);
You are calling run function with some arguments.prcsThread->run(data_child);
In case of QThread run() is the virtual function which we reimplement in our derived class.
so on calling start function run will autometically executed.
The implementation of run() in thread should be without parameters.
Qt Code:
To copy to clipboard, switch view to plain text mode
ex
Qt Code:
{ myThread() {} void setChildData(YourDataType value) { value_ = value } protected: void run() { // DO SOMETHING } private: yourDataType value_; };To copy to clipboard, switch view to plain text mode
you can use this thread
Qt Code:
int myValue_say = 5; myThread *thread = new myThread(); thread->setChildData(myValue_say);To copy to clipboard, switch view to plain text mode
For more basic info about QThread:
http://qt-project.org/doc/qt-4.8/threads-starting.html
where should I use these parts of code:
well in fact I have a common header as a class and I want to share this header between the threads.Qt Code:
myThread *thread = new myThread();To copy to clipboard, switch view to plain text mode
and also I need threads to inform each others about the changes that they have made in the shared data.
you can initialize thread in constructor of your window,
that was just for your understanding about QThread.
for basic information about threads, use Google, it will surely help you.
For data sharing between threads, you can use Global variables , static variables., or you can use a class which is derived with QObject.
Both threads can use the same class.
Use SIGNAL, and SLOTS for notification of data change.
Last edited by karankumar1609; 14th August 2013 at 07:54.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
Bookmarks