QT Multiple threads for data transfer

Source: Internet
Author: User
Tags mutex

in the usual project program, it is often necessary to process multipleSerial Portand the network sent overData, and the data volume is relatively large, 9600 baud rate at least 1000 bytes per second data needs to be processed and reflected inInterface, the same thread that started directly with the UI thread, ran on the x86 machine .problem, after all, X86 the minimum frequency of the machine will not be less than 1.6G, but if the amount of data is larger or to the arm run, the direct UI stuck, think of the solution is to use multi-threading, a thread responsible for collecting data, a thread responsible for processing data, when the protocol, if necessary to the data parsing from the serial port to the network data, the previous method is to re-write a TCP communication processing, this is not a very reasonable approach, after all, the resolution protocol is the same, so I summed up a general idea of data processing: a variety of information received in the form of the back row of the queue into a global variable, Open a separate thread to read the first data from this global variable, and then remove the first data, the list of QT directly provides a takefirst function, very cool! Use the while loop to read and lock while reading, so that there is no conflict.
prototype:
Global Variablesfile:

Copy Code

    1. #ifndef App_h

    2. #define App_h


    3. #include "qstring.h"

    4. #include "qstringlist.h"


    5. Class APP

    6. {

    7. Public

    8. Static Qstringlist list;


    9. };


    10. #endif//app_h


Copy Code

    1. #include "app.h"


    2. Qstringlist app::list=qstringlist ();


Processing Data Threads independently:

Copy Code

  1. #ifndef Test_h

  2. #define Test_h


  3. #include "qthread.h"

  4. #include "qmutex.h"


  5. Class Thread:public Qthread

  6. {

  7. Q_object

  8. Public

  9. Thread ();

  10. ~thread ();


  11. void Stop ();


  12. Protected

  13. void Run ();


  14. Private

  15. Qmutex mutex;

  16. volatile BOOL stopped;


  17. Signals:

  18. void Readone (QString txt);


  19. };


  20. #endif//test_h


Copy Code

  1. #include "thread.h"

  2. #include "app.h"


  3. Thread::thread ()

  4. {

  5. stopped=false;

  6. }


  7. Thread::~thread ()

  8. {


  9. }


  10. void Thread::stop ()

  11. {

  12. stopped=true;

  13. }


  14. void Thread::run ()

  15. {

  16. While (!stopped) {

  17. Mutex.lock ();

  18. If (app::list.count () >0) {

  19. QString Txt=app::list.takefirst ();

  20. Emit Readone (txt);

  21. }

  22. Mutex.unlock ();

  23. Msleep (1);//do Not add this sentence CPU usage up to 50%

  24. }

  25. stopped=false;

  26. }


Main interface:

Copy Code

  1. #ifndef Widget_h

  2. #define Widget_h


  3. #include <QWidget>

  4. #include "thread.h"

  5. #include "qtimer.h"


  6. Namespace Ui {

  7. Class frmmain;

  8. }


  9. Class Frmmain:public Qwidget

  10. {

  11. Q_object


  12. Public

  13. Explicit frmmain (qwidget *parent = 0);

  14. ~frmmain ();


  15. Private Slots:

  16. void Writeone ();

  17. void Readone (QString txt);

  18. void on_btnappend_clicked ();

  19. void on_btnthread_clicked ();

  20. void on_btntimer_clicked ();


  21. Private

  22. Ui::frmmain *ui;


  23. Qtimer *timer;

  24. Thread *thread;


  25. };


  26. #endif//widget_h


Copy Code

  1. #include "frmmain.h"

  2. #include "ui_frmmain.h"

  3. #include "app.h"

  4. #include "qdatetime.h"

  5. #include "qdesktopwidget.h"


  6. #define _TIME_ qprintable (qtime::currenttime (). toString ("now:hh:mm:ss:zzz"))


  7. Frmmain::frmmain (qwidget *parent):

  8. Qwidget (parent),

  9. UI (new Ui::frmmain)

  10. {

  11. Ui->setupui (this);


  12. This->showmaximized ();


  13. Timer=new Qtimer (this);

  14. Timer->setinterval (50);

  15. Connect (timer,signal ()-timeout ()), this,slot (writeone ()));


  16. Thread=new Thread;

  17. Connect (thread,signal (readone (QString)), this,slot (readone (QString)));

  18. }


  19. Frmmain::~frmmain ()

  20. {

  21. Delete ui;

  22. }


  23. void Frmmain::writeone ()

  24. {

  25. App::list.append (_time_);

  26. }


  27. void Frmmain::readone (QString Txt)

  28. {

  29. Ui->txtout->append (txt);

  30. }


  31. void frmmain::on_btnappend_clicked ()

  32. {

  33. App::list.append (ui->txtin->text ());

  34. }


  35. void frmmain::on_btnthread_clicked ()

  36. {

  37. If (ui->btnthread->text () = = "start Thread") {

  38. Thread->start ();

  39. Ui->btnthread->settext ("stop thread");

  40. Ui->txtout->append ("start Thread ok");

  41. }else{

  42. Thread->stop ();

  43. Ui->btnthread->settext ("start thread");

  44. Ui->txtout->append ("stop Thread ok");

  45. }

  46. }


  47. void frmmain::on_btntimer_clicked ()

  48. {

  49. If (ui->btntimer->text () = = "start Timer") {

  50. Timer->start ();

  51. Ui->btntimer->settext ("stop timer");

  52. Ui->txtout->append ("start Timer ok");

  53. }else{

  54. Timer->stop ();

  55. Ui->btntimer->settext ("start timer");

  56. Ui->txtout->append ("stop Timer ok");

  57. }

  58. }


in order to simulate a large amount of data, I opened a timer here for 50 milliseconds, timed to generate the current time string data into a global variable, and then placed severalButtonused to manually add strings and start stopping threads and Timers.
650) this.width=650; "src=" http://www.qtcn.org/bbs/attachment/Mon_1501/44_110085_67de59347a0fe38.jpg?143 " border= "0" style= "border:0px;" alt= "44_110085_67de59347a0fe38.jpg?143"/>


Welcome to criticize suggestions and guidance! Thank you!


QT Multiple threads for data transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.