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

Thread: best threading solution?

  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 16: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)

  10. #9
    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?

    thanks for your answers and useful link.
    i need a timer or thread for downloading (and of course you say timer, good its easier with timer for me ) because its most run 2 times in hour.
    so you say i better to cancel downloading if its not run in needed time or ignore it because of run fastly and async (and ofcourse if my file size isn't so big).
    thanks alot for your help


    Added after 7 minutes:


    and sorry last question if i set interval of my main timer to 0, i shouldnt have terrible right? and my cpu usage is less than 100% right?


    Added after 28 minutes:


    and sorry last question if i set interval of my main timer to 0, i shouldnt have terrible right? and my cpu usage is less than 100% right?
    Last edited by danics; 14th August 2013 at 14:15.

  11. #10
    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
    and sorry last question if i set interval of my main timer to 0, i shouldnt have terrible right? and my cpu usage is less than 100% right?
    If you set the interval to 0 then CPU usage will be high. But why do you want to set the interval to 0?
    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.


  12. #11
    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 think with qtimer everything run in a queue so why doesnt decrease of intervals? just that

  13. #12
    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 think with qtimer everything run in a queue so why doesnt decrease of intervals? just that
    Why would you want to decrease the interval?
    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.


  14. #13
    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?

    reading serial port faster for getting more data if available but my program must run reasonable

  15. #14
    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
    reading serial port faster for getting more data if available but my program must run reasonable
    The data comes at some set speed. Reading faster will not make you read more data than the sender sends. If the serial speed is 115200bps which is 14400Bps which is 14kBps then if you read every 50ms, you can read at most ~700 bytes at a time all this assuming the sender satitates the port completely (which is rarely the case). Then you have to think how long the data remains valid -- if you read it a second later, will the data still be valid? If so, then why read more often than 1 time per second? The user will not notice more frequent updates to the UI anyway. But maybe you can read once every 2 seconds? What about once per 5 seconds?

    "Faster" does not mean "better" and very often it means "worse".
    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. #15
    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?

    no i say my dowork() takes that time not interval, i mean i send 5 or 6 command and wait until they reply so maybe my dowork() get 1 second untill all data that i needed read from board, but my electronic device has a avr with 8mhz clock and so have data every time i need that data (not every time ofcourse but i think every 80msec, i dont know exactly? )


    Added after 48 minutes:


    and one another question , i cant run a function like my dowork() atomic? (no big thread or process from system or my program interupt it).
    Last edited by danics; 14th August 2013 at 17:43.

  17. #16
    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 cant run a function like my dowork() atomic?
    In general no. But why would you want it to be atomic?
    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.


  18. #17
    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?

    nothing just make sure dont lose data. ofcourse not in this sample, like a process run realy in real time mode

  19. #18
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: best threading solution?

    2 danics,

    In your case there is no need for threads and device polling. It is necessary to use event-based solution instead of polling.

    For these purposes you can use QtSerialPort (need do manual build and install if you use Qt 4.8.x, or nothing to do if you use Qt 5.1):

    For Qt 4.8.x
    For Qt 5.1

  20. #19
    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 said not in this sample , i mean using somthing like PCi Slot and read from pci bus not serialport. its just a question: can we block some function or process to run realtime (without block).

  21. #20
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: best threading solution?

    Ahh.. clear.

    In any case you can use event-based approach, irrespective of device type.
    I assume that in drivers for your custom device it "event-based feature" is implemented?...
    (Then everything will be resolved much more simply and more safely. )

Similar Threads

  1. Threading issue in DLL
    By CristonDK in forum Qt Programming
    Replies: 6
    Last Post: 15th June 2012, 19:07
  2. Threading with rsh and rcp
    By jaxrpc in forum Newbie
    Replies: 2
    Last Post: 4th June 2010, 12:50
  3. Threading...?
    By sekatsim in forum Qt Programming
    Replies: 12
    Last Post: 10th June 2008, 02:14
  4. Qt Threading
    By avh in forum Newbie
    Replies: 7
    Last Post: 30th May 2008, 21:20
  5. Sub-Threading
    By TheGrimace in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2007, 17: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.