Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Multithreading

  1. #1
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Multithreading

    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?

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

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

  3. #3
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    yes I am sure here is the code:

    Qt Code:
    1. void Dialog::on_pushButton_Start_clicked()
    2. {
    3. //Started
    4.  
    5.  
    6. if(rThread->Stop == true)
    7. rThread->Stop = false;
    8.  
    9. rThread->start(rThread->HighPriority);
    10.  
    11.  
    12.  
    13. //My_Initializer();
    14.  
    15. if(rcvThread->Stop == true)
    16. rcvThread->Stop = false;
    17.  
    18. rcvThread->start(rcvThread->HighestPriority);
    19. rcvThread->run(data_child);
    20.  
    21.  
    22.  
    23.  
    24.  
    25. if(prcsThread->Stop == true)
    26. prcsThread->Stop = false;
    27.  
    28. prcsThread->start(prcsThread->HighPriority);
    29. prcsThread->run(data_child);
    30.  
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

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


  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    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?


  6. #6
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multithreading

    run() method of QThread will automatically called whenever "start()" method of QThread called.

    Qt Code:
    1. void Dialog::on_pushButton_Start_clicked()
    2. {
    3. //Started
    4.  
    5.  
    6. if(rThread->Stop == true)
    7. rThread->Stop = false;
    8.  
    9. rThread->start(QThread::HighPriority);
    10.  
    11.  
    12.  
    13. //My_Initializer();
    14.  
    15. if(rcvThread->Stop == true)
    16. rcvThread->Stop = false;
    17.  
    18. // data_child should passed before starting of thread
    19. rcvThread->setChildData(data_child);
    20. rcvThread->start(QThread::HighestPriority);
    21. // rcvThread->run(data_child);
    22.  
    23.  
    24.  
    25.  
    26.  
    27. if(prcsThread->Stop == true)
    28. prcsThread->Stop = false;
    29.  
    30. // data_child should passed before starting of thread
    31. prcsThread->setChildData(data_child);
    32. prcsThread->start(QThread::HighPriority);
    33. // prcsThread->run(data_child);
    34.  
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 

    Hope this will help you

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    Quote Originally Posted by havij000 View Post
    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,
    _

  8. #8
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    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
    Attached Files Attached Files
    Last edited by havij000; 13th August 2013 at 06:27.

  9. #9
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    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,

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

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

    You probably want to call your run functions from it.

    Cheers,
    _

  11. #11
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    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?
    Qt Code:
    1. void Recive_Thread::run(Data_Class *data_rcv_child)
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multithreading

    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.

    Qt Code:
    1. rcvThread->start(QThread::HighestPriority);
    To copy to clipboard, switch view to plain text mode 

    ex
    Qt Code:
    1. class myThread : public QThread
    2. {
    3. myThread() {}
    4.  
    5. void setChildData(YourDataType value) {
    6. value_ = value
    7. }
    8.  
    9. protected:
    10. void run()
    11. {
    12. // DO SOMETHING
    13. }
    14.  
    15. private:
    16. yourDataType value_;
    17. };
    To copy to clipboard, switch view to plain text mode 

    you can use this thread
    Qt Code:
    1. int myValue_say = 5;
    2. myThread *thread = new myThread();
    3. thread->setChildData(myValue_say);
    4. thread->start(QThread::HighestPriority);
    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

  13. #13
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    where should I use these parts of code:

    Qt Code:
    1. myThread *thread = new myThread();
    To copy to clipboard, switch view to plain text mode 
    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.

  14. #14
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Multithreading

    Quote Originally Posted by havij000 View Post
    where should I use these parts of code:
    Qt Code:
    1. myThread *thread = new myThread();
    To copy to clipboard, switch view to plain text mode 
    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, 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 06: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.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading

    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    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

  17. #17
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    Quote Originally Posted by havij000 View Post
    Dear buddy,
    Isn't it enough to implement the run function?
    Qt Code:
    1. 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
    Qt Code:
    1. void run();
    To copy to clipboard, switch view to plain text mode 
    Now look at your run function
    Qt Code:
    1. void run(Data_Class *data_rcv_child);
    To copy to clipboard, switch view to plain text mode 
    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,
    _

  18. #18
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    yes I got it.
    you mean that I should emplement the run function:
    Qt Code:
    1. void run();
    To copy to clipboard, switch view to plain text mode 
    and define another function to get and set the values that I need to set!?
    Regards,

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading

    Quote Originally Posted by havij000 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #20
    Join Date
    Nov 2012
    Posts
    48
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading

    Dear wysota,

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

Similar Threads

  1. When to use Multithreading?
    By "BumbleBee" in forum General Programming
    Replies: 5
    Last Post: 30th April 2011, 18:21
  2. QThread for Multithreading
    By strateng in forum Newbie
    Replies: 12
    Last Post: 8th June 2010, 13:32
  3. regarding multithreading
    By mohanakrishnan in forum Qt Programming
    Replies: 19
    Last Post: 9th December 2009, 08:21
  4. multithreading
    By mickey in forum General Programming
    Replies: 2
    Last Post: 5th June 2008, 22:01
  5. MultiThreading in Qt
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2006, 19:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.