Results 1 to 8 of 8

Thread: Occurrence Delay in qtconcurrent therds

  1. #1
    Join Date
    Oct 2010
    Location
    Iran
    Posts
    27
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Exclamation Occurrence Delay in qtconcurrent therds

    Hello
    I create three Threads with QtConcurrent :

    1- First Thread connect to devise (with Qtimer interval 0.05 seconds and set AvailableValue = 1 if 20 value is reading )

    2- Second Thread Control AvailableValue == 1 (in per seconds) and draw graph (qpainter event with Qtimer interval 1 seconds)
    (Value saving in qlist )

    2- Third Thread (in 5 minut) save value to mysql db (qpainter event with Qtimer interval 300 seconds)

    problem

    run Second Thread Or Third Thread ,Occurrence delay in First Thread
    (if use mouse event in Second Thread, further delay)

    How to Resolve this problem?

    Code:

    Qt Code:
    1. Read_timer = new QTimer();
    2. draw_timer = new QTimer();
    3.  
    4. FirstThread = new QFuture<void>;
    5. watcher3 = new QFutureWatcher<void>;
    6. * FirstThread = QtConcurrent::run(this,&MainWindow::Devs);
    7. watcher3->setFuture(*FirsThread);
    8.  
    9. SecondThread = new QFuture<void>;
    10. watcher2 = new QFutureWatcher<void>;
    11. *SecondThread = QtConcurrent::run(this,&MainWindow::Graph);
    12. watcher2->setFuture(*SecondThread);
    13.  
    14. ThirdThread = new QFuture<void>;
    15. watcher1 = new QFutureWatcher<void>;
    16. *ThirdThread = QtConcurrent::run(this,&MainWindow::Save);
    17. watcher1->setFuture(*ThirdThread);
    18.  
    19. void MainWindow::Devs()
    20. {
    21. connect(Read_timer,SIGNAL(timeout()), this, SLOT(Read_Devs()));
    22. Read_timer->setInterval(50);
    23. Read_timer->start();
    24. }
    25.  
    26. void MainWindow::Read_Devs()
    27. {
    28. {
    29. adv0->adv1716_read(values_float);
    30. adv1->adv1716_read(values_float);
    31. readconter++;
    32. }
    33.  
    34. if(readconter==20)
    35. {
    36. tmpValues_float = values_float;
    37. isAvalaibelValus = 1;
    38. readconter=0;
    39. values_float.clear();
    40. }
    41. }
    42.  
    43. void MainWindow::Graph ()
    44. {
    45. connect(CheckTask_timer,SIGNAL(timeout()), this, SLOT(draw()));
    46. draw_timer->setInterval(1000);
    47. draw_timer->start();
    48. }
    49.  
    50. void MainWindow::Save ()
    51. {
    52. connect(CheckTask_timer,SIGNAL(timeout()), this, SLOT(Save_mysql()));
    53. draw_timer->setInterval(300000);
    54. draw_timer->start();
    55. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Occurrence Delay in qtconcurrent therds

    Use QThread instances instead of QtConcurrent.
    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.


  3. #3
    Join Date
    Oct 2010
    Location
    Iran
    Posts
    27
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Occurrence Delay in qtconcurrent therds

    QtConcurrent Not create real Thead ?!

    Quote Originally Posted by wysota View Post
    Use QThread instances instead of QtConcurrent.
    QtConcurrent Not create real Thead ?!

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

    Default Re: Occurrence Delay in qtconcurrent therds

    QtConcurrent takes a thread from a thread pool. The size of the pool is equal to the number of cores/cpus in your machine. Thus if you run your three-thread code on a less-than-three-core machine (once you correct all the issues mentioned later), it will never complete. The other issue is that you are using signals and slots and incorrectly assuming the slots will be run in the context of threads running the function that emitted the signal. Moreover you assume your timers will fire in the context of the threads running the start() function -- they will not since you don't have an event loop running for each of those threads and timers need events to be handled. Thus your code will do practically nothing apart starting three timers returning immediately (effectively finishing those threads).
    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
    Oct 2010
    Location
    Iran
    Posts
    27
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Occurrence Delay in qtconcurrent therds

    Thanks
    Not enogh time to learn and use QThread , Possible to increase the 'thread pool' size ?
    Is there another solution ØŸ

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

    Default Re: Occurrence Delay in qtconcurrent therds

    Increasing thread pool size will not do you any good. That's the least of your problems. Learn to use QThread properly.

    Qt Code:
    1. QThread *thread = new QThread(this);
    2. thread->start();
    3. MyObject *obj = new MyObject;
    4. obj->moveToThread(thread);
    5. QMetaObject::invokeMethod(obj, "doSomething", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    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.


  7. #7
    Join Date
    Oct 2010
    Location
    Iran
    Posts
    27
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Occurrence Delay in qtconcurrent therds

    Quote Originally Posted by wysota View Post
    Increasing thread pool size will not do you any good. That's the least of your problems. Learn to use QThread properly.

    Qt Code:
    1. QThread *thread = new QThread(this);
    2. thread->start();
    3. MyObject *obj = new MyObject;
    4. obj->moveToThread(thread);
    5. QMetaObject::invokeMethod(obj, "doSomething", Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    QObject::moveToThread: Widgets cannot be moved to a new thread

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

    Default Re: Occurrence Delay in qtconcurrent therds

    QObject::moveToThread: Widgets cannot be moved to a new thread
    That's definitely true. That's why you need to learn to use threads (regardless if you do it with QtConcurrent or with QThread) instead of blindly trying things. At least read the docs for multithreading with Qt.
    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.


Similar Threads

  1. To add delay
    By vinayaka in forum Newbie
    Replies: 0
    Last Post: 3rd June 2011, 13:21
  2. Add Delay.
    By Johncdy in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2011, 08:18
  3. Delay DLL loading ?
    By black_coder in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2009, 00:00
  4. QTimer delay
    By dima in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2009, 13:31

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
  •  
Qt is a trademark of The Qt Company.