PDA

View Full Version : Multithreading



havij000
29th July 2013, 07:51
Hello everybody,

I am new to QT.
I have some different threads which should work from the begining of the programm till the end of it and I need to know that how should I start these different threads simultaneously by cilicking on a single buttom in my ui?
when I start them in the push bottom part of the dialog.cpp they wont start simultaneously the start one after another and as none of them wont stop working till the end of the progromm just one of them works.

how can I run them simultaneously?:confused:

thanks

anda_skoa
29th July 2013, 08:52
Are you sure you actually call start() on all threads?

Can you post the code of the slot you have connected to your button?

Cheers,
_

havij000
5th August 2013, 05:23
yes I am sure here is the code:



void Dialog::on_pushButton_Start_clicked()
{
//Started


if(rThread->Stop == true)
rThread->Stop = false;

rThread->start(rThread->HighPriority);



//My_Initializer();

if(rcvThread->Stop == true)
rcvThread->Stop = false;

rcvThread->start(rcvThread->HighestPriority);
rcvThread->run(data_child);





if(prcsThread->Stop == true)
prcsThread->Stop = false;

prcsThread->start(prcsThread->HighPriority);
prcsThread->run(data_child);


}



but in debuging the code I found that it wont start them simultaneously.

:confused:

wysota
5th August 2013, 07:19
If those are QThread subclasses then calling the run() method directly will not run the function in a thread controlled by the QThread object. You are supposed to just call start() as the docs which I'm sure you must have read clearly states.

By the way, I'm also sure your forum profile has only those platforms and Qt products marked that you actually use as it would be really stupid to make it harder for people trying to help you determine which platform/version you were using.

havij000
12th August 2013, 06:29
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?

:confused:

karankumar1609
12th August 2013, 07:32
run() method of QThread will automatically called whenever "start()" method of QThread called.



void Dialog::on_pushButton_Start_clicked()
{
//Started


if(rThread->Stop == true)
rThread->Stop = false;

rThread->start(QThread::HighPriority);



//My_Initializer();

if(rcvThread->Stop == true)
rcvThread->Stop = false;

// data_child should passed before starting of thread
rcvThread->setChildData(data_child);
rcvThread->start(QThread::HighestPriority);
// rcvThread->run(data_child);





if(prcsThread->Stop == true)
prcsThread->Stop = false;

// data_child should passed before starting of thread
prcsThread->setChildData(data_child);
prcsThread->start(QThread::HighPriority);
// prcsThread->run(data_child);


}


Hope this will help you

anda_skoa
12th August 2013, 08:37
if I want to inform one thread from another thread how should I do it?

Wel, in Qt the easiest way of course is signal/slots, assuming the receiver thread is running its event loop.

Aside from that you can use all object communication options that C++ offers, like calling a method on the other object, etc.

Cheers,
_

havij000
13th August 2013, 05:49
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

havij000
13th August 2013, 08:06
Dear karankumar1609's,
thanks for your reply.

would you please put the complete code here, I really do not understand what is the
prcsThread->setChildData(data_child); is what should it do!

thanks

regards,

anda_skoa
13th August 2013, 11:19
Both of your threads are missing the implementation of QThread::run()

You probably want to call your run functions from it.

Cheers,
_

havij000
14th August 2013, 04:56
Dear buddy,
thanks for your response.



Both of your threads are missing the implementation of QThread::run()


would you please explain it more about it?

Isn't it enough to implement the run function?


void Recive_Thread::run(Data_Class *data_rcv_child)

:confused:

karankumar1609
14th August 2013, 05:13
prcsThread->setChildData(data_child);

It is just the replacement of your code


prcsThread->run(data_child);

You are calling run function with some arguments.
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.



rcvThread->start(QThread::HighestPriority);


ex


class myThread : public QThread
{
myThread() {}

void setChildData(YourDataType value) {
value_ = value
}

protected:
void run()
{
// DO SOMETHING
}

private:
yourDataType value_;
};


you can use this thread


int myValue_say = 5;
myThread *thread = new myThread();
thread->setChildData(myValue_say);
thread->start(QThread::HighestPriority);


For more basic info about QThread:
http://qt-project.org/doc/qt-4.8/threads-starting.html

havij000
14th August 2013, 05:32
where should I use these parts of code:






myThread *thread = new myThread();




well in fact I have a common header as a class and I want to share this header between the threads.
and also I need threads to inform each others about the changes that they have made in the shared data.

karankumar1609
14th August 2013, 05:48
where should I use these parts of code:

myThread *thread = new myThread();
well in fact I have a common header as a class and I want to share this header between the threads.
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 (http://www.google.com), 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.

wysota
14th August 2013, 06:59
Can you tell us why in your opinion you need to use threads? Maybe you could implement your tasks without threads and then if you decide that threads are required, a simple operation of moving objects performing those tasks to threads using QObject::moveToThread().

havij000
14th August 2013, 08:15
Dear wysota,

I want receive data-link data from lan (each packet can be up to 7 ethernet packet) and check them if some of them are not complete drup them and sort the rest of them and show the sorted data in scope in the ui and also send them through a tcp connection to another computer.
Also there are some register that I should set them which I get their values from ui and send the value through a the same raw socket connection.
so I impliment this by below thread:
1. recieve thread: receiving data from LAN
2. process thread: checking and sorting, assemble them and storing packets
3. ui thread which is the main thread.
and a common data structure which implement the shape of the data whiich is as class that I shared it between these threads.

Regards

anda_skoa
14th August 2013, 08:49
Dear buddy,
Isn't it enough to implement the run function?


void Recive_Thread::run(Data_Class *data_rcv_child)



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


void run();

Now look at your run function


void run(Data_Class *data_rcv_child);

Don't they look different?

"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,
_

havij000
14th August 2013, 09:00
yes I got it.
you mean that I should emplement the run function:


void run();

and define another function to get and set the values that I need to set!?

wysota
14th August 2013, 10:37
I want receive data-link data from lan (each packet can be up to 7 ethernet packet) and check them if some of them are not complete drup them and sort the rest of them and show the sorted data in scope in the ui and also send them through a tcp connection to another computer.
Also there are some register that I should set them which I get their values from ui and send the value through a the same raw socket connection.
so I impliment this by below thread:
1. recieve thread: receiving data from LAN
2. process thread: checking and sorting, assemble them and storing packets
3. ui thread which is the main thread.
and a common data structure which implement the shape of the data whiich is as class that I shared it between these threads.


You can perform all those tasks in a single thread. I suggest you implement everything in one (main) thread using signals and slots and only if you decide that you do require threads, move tasks to different threads using the method I mentioned in my previous post.

havij000
17th August 2013, 05:07
Dear wysota,

as the processing is a time consuming task I prefer to do them in different threads.

wysota
19th August 2013, 15:10
Dear wysota,

as the processing is a time consuming task I prefer to do them in different threads.

So do exactly that. See that this is not what you said earlier:


1. recieve thread: receiving data from LAN
2. process thread: checking and sorting, assemble them and storing packets
3. ui thread which is the main thread.

Points 1 and 3 are not related to processing and can thus be done in the same thread. On the other hand if you have 10 tasks to process, queueing them all into a single thread will probably make them process longer than it takes for a next task to flow in. Consider using a pool of threads (e.g. using QtConcurrent::run()) for processing (and processing only).

havij000
21st August 2013, 12:24
I wrote a multi thread programm but after running for 10 seconds I fase the erro : "The program has unexpectedly finished", "exited with code 0"

Who can help me?

:confused:

wysota
21st August 2013, 13:35
What do you expect us to do for you?