Results 1 to 20 of 21

Thread: best threading solution?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default best threading solution?

    Hi,
    first of all my question may be seemed too open, but i think its not ,

    i want to read from a electronic device (from serial port) every 50-100 msec and i think i must added it to a thread i call it main thread, i parse readed data and if i have a good result show some message in my ui,
    i have a menu form in my ui too, if user click every of buttons in ui, my main thread must block.
    so this is what i want: if main thread run Ui block and if ui run main thread block. i have some messaging mechanism with signals and slots too,

    so i must use wait conditions or you have another suggestions?? what you say?

    my main thread each loop get only 1 second time for run and then can sleep 50 msec or set/release a wait conditon i dont know
    i have another thread too if another two threads are block, and if its time of my tasks (like 2 times in hour) for downloading and uploading my results, this one must run but dont block their, priority of those is more than this thread and i must send this thread to sleep,
    on another hand this thread only have 50-100 msec to run and then must go to sleep until main thread finish one loop work and go to sleep.
    but downloading and uploading can run like that????

    and my another question about this thread, how this one must be implemment? i mean use same mechanism probably but i must use a event loop for this thread am i right?
    this is my download command

    Qt Code:
    1. class DownloadCommand : public ICommand
    2. {
    3. Q_OBJECT
    4. public:
    5. DownloadCommand(QObject *parent = 0);
    6. DownloadCommand(const QString& url, QObject* parent=0);
    7.  
    8. void setUrl(const QString& url) { m_url = QUrl(url); }
    9.  
    10. signals:
    11.  
    12. public slots:
    13. void onNetworkReply(QNetworkReply* reply);
    14. void downloadCompleted(qint64 d, qint64 size);
    15. QString execute();
    16.  
    17. private:
    18. QUrl m_url;
    19. QString m_localpath;
    20. QNetworkAccessManager manager;
    21. QNetworkReply* m_reply;
    22. };
    23.  
    24. DownloadCommand::DownloadCommand(QObject *parent) :
    25. ICommand(parent)
    26. {
    27. }
    28.  
    29. DownloadCommand::DownloadCommand(const QString &url, QObject *parent) : ICommand(parent)
    30. {
    31. m_url = QUrl::fromUserInput(url);
    32. }
    33.  
    34. QString DownloadCommand::execute()
    35. {
    36. m_reply = manager.get(QNetworkRequest(m_url));
    37. connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onNetworkReply(QNetworkReply*)));
    38. connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadCompleted(qint64,qint64)));
    39. return tr("Download Started...");
    40. }
    41.  
    42. void DownloadCommand::onNetworkReply(QNetworkReply *reply)
    43. {
    44. if(reply->error() != QNetworkReply::NoError)
    45. return;
    46.  
    47. QByteArray data = reply->readAll();
    48. if(data.length() < 2) return;
    49.  
    50. QFileInfo fileInfo(m_url.path());
    51. QString fileName = fileInfo.fileName();
    52. if(fileName.isEmpty())
    53. fileName = "unknown.file";
    54.  
    55. if(!m_localpath.isNull())
    56. fileName = m_localpath + QDir::separator() + fileName;
    57.  
    58. if(QFile::exists(fileName))
    59. {
    60. int i = 0;
    61. QString newfileName = fileName + ".";
    62.  
    63. while(QFile::exists(newfileName + QString::number(i)))
    64. ++i;
    65.  
    66. newfileName += QString::number(i);
    67. QFile fm(fileName);
    68. fm.rename(newfileName);
    69. }
    70.  
    71. QFile file(fileName);
    72. if(file.open(QFile::WriteOnly))
    73. file.write(data);
    74.  
    75. reply->deleteLater();
    76. m_reply->deleteLater();
    77. }
    78.  
    79. void DownloadCommand::downloadCompleted(qint64 d, qint64 size)
    80. {
    81. if((d == 0 && size == 0) || (m_reply->error() != QNetworkReply::NoError))
    82. emit processFinished(tr("Cant download file error is %1").arg(m_reply->errorString()));
    83. else
    84. {
    85. if(size != -1)
    86. emit processChanges(100 * d / size);
    87. else emit processChanges(-1);
    88. if(size == d)
    89. emit processFinished(tr("Download is complete"));
    90. }
    91. }
    To copy to clipboard, switch view to plain text mode 
    (i copy this code for dont say me give us some code man ) .)

    so i think move it to MyThread and then run myThread.exec() every time that i need to download file. ( i ask this question for correct running signals and slots in thread)

    thanks.

  2. #2
    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: best threading solution?

    Use a timer instead of a thread.
    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
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: best threading solution?

    i use QTimer before but its get so cpu usage for 50 msec call, am i right? this program run on a embedded system

    and how can i handle download thread?

  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: best threading solution?

    If you use a thread it will also bump up your cpu usage. If you need to do something, it will have to be done by the cpu regardless if you use a thread or not.
    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
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: best threading solution?

    i dont mean that, i mean impletation of Qtimer for small intervals may be is not so good i dont remember where i read this so i ask,
    and if i use a timer for my main loop how can i run a task thread for downloading and uploading my data?
    you say a timer exactly run in its interval time and sleep other threads? ( i think timeout signal go to queue so my other threads can delay running main process)
    Last edited by danics; 13th August 2013 at 15:15.

  6. #6
    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: best threading solution?

    Quote Originally Posted by danics View Post
    i dont mean that, i mean impletation of Qtimer for small intervals may be is not so good i dont remember where i read this so i ask,
    Ask the one who wrote it

    and if i use a timer for my main loop how can i run a task thread for downloading and uploading my data?
    You don't need a thread for that.

    you say a timer exactly run in its interval time and sleep other threads?
    The timer has no influence on other threads.

    ( i think timeout signal go to queue so my other threads can delay running main process)
    Look, it is very simple -- if your program has to do two tasks which take 10 and 20 units of the processor time then regardless if you use threads or not, you will have to spend 30 units of processor time on those tasks. If your machine can do more tasks in parallel than there are tasks waiting to execute (e.g. the processor has multiple cores) then the real time used to complete both tasks (assuming they are independent) will take less than 30 units of time. If your machine has less concurrent threads than there are tasks to execute then the total amount of time spent will be more than those 30 units because the processor will need to switch between tasks. Considering the system has to do more than just handle your two-threaded application, it is very likely it has more tasks to do than threads available. In that case adding more and more threads will slow down the process rather than speeding it up.
    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
    Mar 2010
    Posts
    77
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: best threading solution?

    ok let me explain more, i want to know witch solution is better,
    first of all i know i need 30 units, but i have priority for switch task, i need my main thread run with download thread somehow simultaneity but if sleep time of main thread finished the download thread must go to sleep and wait to up until main thread again finish, so in your explanation we cant send a signal to a thread to sleep and call another thread to run, we can do this right? and if you say yes, how much this process better than use timers (in timers mode probably main thread dont run until download thread finish)

    we have a loop for main thread and we set a wait condition this is main loop

    while(1) {
    waitOn(); // check if Ui is in waiting state and if its not set uis and download to wait
    doWork();
    setOn() // set wait to free
    sleep(100);
    }

    and our problem is download thread, its not run sync so i dont know how implement it?
    and finally you dont say if i send my sample download command i a thread object and then call exec() function of that thread everything works in event loop of that thread right?

  8. #8
    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: best threading solution?

    You don't need any "download thread". It's not that there is a dwarf sitting there and constantly pulling a string that has your incoming data attached to it and which needs constant bugging to get to work. When the data arrives into the socket you will be informed about it, you can read the data (which takes literally miliseconds) and afterwards you can attend to other tasks. When more data arrives, you will again be informed about it, you will read the data and go back to the other task. On the other hand if you do it with threads, your thread will sit around doing nothing 99% of the time and you will have a lot of trouble in synchronizing all your threads.

    Your while() loop can be decomposed into a simple timer running with 100ms interval that triggers a slot that calls "doWork()".

    Here, read this: [wiki]Keeping the GUI Responsive[/wiki]
    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.


  9. The following user says thank you to wysota for this useful post:

    danics (14th August 2013)

Similar Threads

  1. Threading issue in DLL
    By CristonDK in forum Qt Programming
    Replies: 6
    Last Post: 15th June 2012, 18:07
  2. Threading with rsh and rcp
    By jaxrpc in forum Newbie
    Replies: 2
    Last Post: 4th June 2010, 11:50
  3. Threading...?
    By sekatsim in forum Qt Programming
    Replies: 12
    Last Post: 10th June 2008, 01:14
  4. Qt Threading
    By avh in forum Newbie
    Replies: 7
    Last Post: 30th May 2008, 20:20
  5. Sub-Threading
    By TheGrimace in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2007, 16:38

Tags for this Thread

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.