In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about how to achieve simple multithreading in Qt5.9. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Qt starts multithreading, mainly using class QThread. There are two ways. The first is to inherit QThread with a class, and then rewrite the virtual function run (). When you want to start a new thread, you just need to instantiate the class, and then call the function start () to start a multithread. The second method is to inherit a QObject class and then use the moveToThread () function to open a thread slot function in which the code that takes a lot of time to calculate is put into it. The second method can refer to another blog I wrote: https://www.yisu.com/article/223796.htm
What I summarize below is mainly the first method. (note: only in the run () function is the new thread, and all complex logic should be done in the run () function. When the run () function finishes running, the thread's life cycle ends. )
The steps to create multithreading are as follows:
A1 create a new class MyThread, and the base class is QThread.
A2 overrides the virtual function void run () of the class MyThread, that is, create a new function protected void run (), and then define it.
A3 where multithreading is needed, instance MyThread, and then call the function MyThread::start (), then start a thread and automatically run the function run ().
A4 when a thread is stopped, the MyThread::wait () function is called, waiting for the thread to end, and reclaiming thread resources.
1.1 create a new widget project, do not check the ui interface. Then add the following code to mythread.h,mythread.cpp,widget.h,widget.cpp,main.cpp respectively.
Mythread.h
# ifndef MYTHREAD_H#define MYTHREAD_H# include class MyThread: public QThread {public: MyThread (); void closeThread (); protected: virtual void run (); private: volatile bool isStop; / / isStop is a volatile variable and needs to be declared in volatile}; # endif / / MYTHREAD_H
Mythread.cpp
# include "mythread.h" # include # include MyThread::MyThread () {isStop = false;} void MyThread::closeThread () {isStop = true;} void MyThread::run () {while (1) {if (isStop) return; qDebug () start (); qDebug ()
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.