Qt thread Summary (1) Qt simple thread and qt thread Summary

Source: Internet
Author: User

Qt thread Summary (1) Qt simple thread and qt thread Summary

This series is a summary of the use of a period of time, including thread creation, inter-thread communication, etc., write a little bit every day.

The first section describes how to create and use a thread.

First of all, these articles are mainly intended for newcomers. Because Qt is simple and easy to use, many newcomers often use Qt when they are new to programming, the thread concept is a bit vague, so it is necessary to briefly describe it.

Threads are widely used in programs and are often used to avoid program blocking, distributed computing, multi-task collaboration, and other functions.

Some friends like to generate dozens or even hundreds of threads in a program to achieve the effect of no program blocking and improving the running efficiency. However, you must note that, in many cases, too many threads cannot improve the efficiency. If the thread does not sleep or wait, the maximum number of concurrent running tasks is the number of CPU cores. The CPU needs to be switched and scheduled among many threads, this reduces system efficiency. In another case, if the thread waits for most of the time (such as waiting for a return or reading the hard disk), a slight increase in the number will indeed increase the efficiency.

So to sum up:

How to create and use threads in Qt is very simple. It can be divided into three steps:

(1) first, create a Qt command line project and create a Thread class in the project:

CSimpleThread. h

 1 #ifndef CSIMPLETHREAD_H 2 #define CSIMPLETHREAD_H 3 #include <QThread> 4  5 class CSimpleThread : public QThread 6 { 7     Q_OBJECT 8 public: 9     CSimpleThread();10 };11 12 #endif // CSIMPLETHREAD_H

CSimpleThread. cpp

1 #include "CSimpleThread.h"2 #include <QDebug>3 CSimpleThread::CSimpleThread()4 {5 6 }

(2) rewrite run () to print a sentence every 5 seconds

CSimpleThread. h

 1 #ifndef CSIMPLETHREAD_H 2 #define CSIMPLETHREAD_H 3 #include <QThread> 4  5 class CSimpleThread : public QThread 6 { 7     Q_OBJECT 8 public: 9     CSimpleThread();10     void run();11 };12 13 #endif // CSIMPLETHREAD_H

 

CSimpleThread. cpp

 1 #include "CSimpleThread.h" 2 #include <QDebug> 3 CSimpleThread::CSimpleThread() 4 { 5  6 } 7  8 void CSimpleThread::run() 9 {10     while (true) {11         qDebug()<<"CSimpleThread run!";12         sleep(5);13     }14 }

(3) create a thread object in the main thread and start the thread using the start () method.

Main. cpp

 1 #include <QCoreApplication> 2 #include <CSimpleThread.h> 3  4 int main(int argc, char *argv[]) 5 { 6     QCoreApplication a(argc, argv); 7  8     CSimpleThread *SThread = new CSimpleThread(); 9     SThread->start();10 11     return a.exec();12 }

Compile and run the program. The thread runs in the background and prints a sentence every five seconds.

 

The next content is how to communicate with the thread.

Related Article

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.