当前位置:网站首页>QT uses semaphores to control threads (qsemaphore)
QT uses semaphores to control threads (qsemaphore)
2022-07-28 07:59:00 【Emilia sugar】
What you want to do : The execution sequence of multiple threads is controlled by semaphores . For example, use three threads , Print the characters separately in order ‘A’、‘B’、‘C’. I will use two ways to create threads to achieve the above operation , The first kind of inheritance QThread again run Method , The second way is through QFeature To achieve .
Program directory :

1. Create a Widget Interface , It is used to create threads in two ways

Interface Widget Code for :
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "qthreadTest.h"
#include "qfeaturethread.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_feature_clicked();
void on_pushButton_thread_clicked();
private:
Ui::Widget *ui;
qThreadTest threadTest; // Inherit QThread rewrite run Function mode
qFeatureThread featureTest; // Use QFeature The way
};
#endif // WIDGET_Hwidget.cpp:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_feature_clicked()
{
featureTest.initThread();
}
void Widget::on_pushButton_thread_clicked()
{
threadTest.initThread();
}qthreadTest.h:
#ifndef QTHREADTEST_H
#define QTHREADTEST_H
#include <QObject>
#include <QThread>
#include <QMutex>
// Threads 1 Class
class qThread1 : public QThread
{
Q_OBJECT
public:
explicit qThread1(QObject* parent = 0);
~qThread1();
void stop();
protected:
void run();
private:
volatile bool stopped = false;
};
// Threads 2 Class
class qThread2 : public QThread
{
Q_OBJECT
public:
explicit qThread2(QObject* parent = 0);
~qThread2();
void stop();
protected:
void run();
private:
volatile bool stopped = false;
};
// Threads 3 Class
class qThread3 : public QThread
{
Q_OBJECT
public:
explicit qThread3(QObject* parent = 0);
~qThread3();
void stop();
protected:
void run();
private:
volatile bool stopped = false;
};
class qThreadTest : public QObject
{
Q_OBJECT
public:
explicit qThreadTest(QObject *parent = 0);
~qThreadTest();
void initThread();
signals:
public slots:
private:
qThread1 thread1;
qThread2 thread2;
qThread3 thread3;
};
#endif // QTHREADTEST_HqthreadTest.cpp:
#include "qthreadTest.h"
#include <QDebug>
#include <QSemaphore>
static QSemaphore smt_used0;
static QSemaphore smt_used1;
static QSemaphore smt_used2(1);
static char ch = 'A';
qThreadTest::qThreadTest(QObject *parent) : QObject(parent)
{
}
qThreadTest::~qThreadTest()
{
if(thread1.isRunning())
thread1.stop();
if(thread2.isRunning())
thread2.stop();
if(thread3.isRunning())
thread3.stop();
}
void qThreadTest::initThread()
{
if(!thread1.isRunning())
thread1.start();
if(!thread2.isRunning())
thread2.start();
if(!thread3.isRunning())
thread3.start();
}
qThread1::qThread1(QObject *parent)
{
stopped = false;
qDebug() << QString::fromLocal8Bit(" Threads thread1 Class constructed ");
}
qThread1::~qThread1()
{
stop();
qDebug() << QString::fromLocal8Bit(" Threads thread1 Deconstruction ");
}
void qThread1::stop()
{
stopped = true;
}
void qThread1::run()
{
qDebug() << QString::fromLocal8Bit(" Threads thread1 Be opened ");
while(!stopped)
{
smt_used0.acquire();
qDebug() << ch << "\n";
char cd = ++ch; // When debugging, you can see cd value
if('D' == ch)
ch = 'A';
_sleep(1000);
smt_used1.release();
}
stopped = false;
}
qThread2::qThread2(QObject *parent)
{
stopped = false;
qDebug() << QString::fromLocal8Bit(" Threads thread2 Class constructed ");
}
qThread2::~qThread2()
{
stop();
qDebug() << QString::fromLocal8Bit(" Threads thread2 Deconstruction ");
}
void qThread2::stop()
{
stopped = true;
}
void qThread2::run()
{
qDebug() << QString::fromLocal8Bit(" Threads thread2 Be opened ");
while(!stopped)
{
smt_used1.acquire();
qDebug() << ch << "\n";
char cd = ++ch;
if('D' == ch)
ch = 'A';
_sleep(1000);
smt_used2.release();
}
stopped = false;
}
qThread3::qThread3(QObject *parent)
{
stopped = false;
qDebug() << QString::fromLocal8Bit(" Threads thread3 Class constructed ");
}
qThread3::~qThread3()
{
stop();
qDebug() << QString::fromLocal8Bit(" Threads thread3 Deconstruction ");
}
void qThread3::stop()
{
stopped = true;
}
void qThread3::run()
{
qDebug() << QString::fromLocal8Bit(" Threads thread3 Be opened ");
while(!stopped)
{
smt_used2.acquire();
qDebug() << ch << "\n";
char cd = ++ch;
if('D' == ch)
ch = 'A';
_sleep(1000);
smt_used0.release();
}
stopped = false;
}qfeaturethread.h:
#ifndef QFEATURETHREAD_H
#define QFEATURETHREAD_H
#include <QObject>
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrent>
class qFeatureThread : public QObject
{
Q_OBJECT
public:
explicit qFeatureThread();
~qFeatureThread();
void initThread();
private:
bool b_finish = false;
bool b_finish1 = false;
bool b_finish2 = false;
QFuture<int> future;
QFuture<int> future1;
QFuture<int> future2;
QFutureWatcher<int>* watcher = nullptr;
QFutureWatcher<int>* watcher1 = nullptr;
QFutureWatcher<int>* watcher2 = nullptr;
};
#endif // QFEATURETHREAD_Hqfeaturethread.cpp:
#include "qfeaturethread.h"
#include <QSemaphore>
#include <QDebug>
static QSemaphore smt_used0;
static QSemaphore smt_used1;
static QSemaphore smt_used2(1);
static char ch = 'A';
qFeatureThread::qFeatureThread()
{
}
qFeatureThread::~qFeatureThread()
{
if(watcher != nullptr && watcher->future().isRunning())
{
b_finish = true;
watcher->future().waitForFinished();
watcher = nullptr;
}
if(watcher1 != nullptr && watcher1->future().isRunning())
{
b_finish1 = true;
watcher1->future().waitForFinished();
watcher1 = nullptr;
}
if(watcher2 != nullptr && watcher2->future().isRunning())
{
b_finish2 = true;
watcher2->future().waitForFinished();
watcher2 = nullptr;
}
}
void qFeatureThread::initThread()
{
if(watcher == nullptr)
{
b_finish = false;
// Monitor the end of the thread
watcher = new QFutureWatcher<int>;
connect(watcher, &QFutureWatcher<int>::finished,[=](){
qDebug() << QString::fromLocal8Bit(" Threads A Has stopped ");
watcher->deleteLater();
});
// Start a new thread
future = QtConcurrent::run([=]() mutable {
while(1)
{
smt_used0.acquire();
qDebug() << ch << "\n";
char cd = ++ch; // When debugging, you can see cd value
if('D' == ch)
ch = 'A';
_sleep(1000);
smt_used1.release();
if(b_finish)
break;
}
return 0;
});
qDebug() << QString::fromLocal8Bit(" Threads A Started ");
watcher->setFuture(future);
}
else if(watcher != nullptr && watcher->future().isRunning())
{
b_finish = true;
watcher->future().waitForFinished();
watcher = nullptr;
qDebug() << QString::fromLocal8Bit(" Threads A Has stopped ");
}
if(watcher1 == nullptr)
{
b_finish1 = false;
// Monitor the end of the thread
watcher1 = new QFutureWatcher<int>;
connect(watcher1, &QFutureWatcher<int>::finished,[=](){
qDebug() << QString::fromLocal8Bit(" Threads B Has stopped ");
watcher1->deleteLater();
});
future1 = QtConcurrent::run([=]() mutable {
while(1)
{
smt_used1.acquire();
qDebug() << ch << "\n";
char cd = ++ch;
if('D' == ch)
ch = 'A';
_sleep(1000);
smt_used2.release();
if(b_finish1)
break;
}
return 0;
});
qDebug() << QString::fromLocal8Bit(" Threads B Started ");
watcher1->setFuture(future1);
}
else if(watcher1 != nullptr && watcher1->future().isRunning())
{
b_finish1 = true;
watcher1->future().waitForFinished();
watcher1 = nullptr;
qDebug() << QString::fromLocal8Bit(" Threads B Has stopped ");
}
if(watcher2 == nullptr)
{
b_finish2 = false;
// Monitor the end of the thread
watcher2 = new QFutureWatcher<int>;
connect(watcher2, &QFutureWatcher<int>::finished,[=](){
qDebug() << QString::fromLocal8Bit(" Threads C Has stopped ");
watcher2->deleteLater();
});
// Start a new thread
future2 = QtConcurrent::run([=]() mutable {
while(1)
{
smt_used2.acquire();
qDebug() << ch << "\n";
char cd = ++ch;
if('D' == ch)
ch = 'A';
_sleep(1000);
smt_used0.release();
if(b_finish2)
break;
}
return 0;
});
qDebug() << QString::fromLocal8Bit(" Threads C Started ");
watcher2->setFuture(future2);
}
else if(watcher2 != nullptr && watcher2->future().isRunning())
{
b_finish2 = true;
watcher2->future().waitForFinished();
watcher2 = nullptr;
qDebug() << QString::fromLocal8Bit(" Threads C Has stopped ");
}
}
《 let's row our boats 》
边栏推荐
- (daily question) - the longest substring without repeated characters
- Learn software testing in two weeks? I was shocked!
- EMC中class A和class B哪个更严格?
- awk从入门到入土(16)awk变量类型探讨--关于数字和string两种类型
- mysql:LIKE和REGEXP操作有什么区别?
- EMC rectification ideas
- 【17】 Establish data path (upper): instruction + operation =cpu
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2 supplementary question record (dghjkl)
- 演讲笔记 适合所有人的实用程序生成 PCG
- MySQL view the memory size of a table
猜你喜欢

【17】 Establish data path (upper): instruction + operation =cpu

细说共模干扰和差模干扰

辨析覆盖索引/索引覆盖/三星索引

常用电子产品行业标准及认证

Industry standards and certification of common electronic products
C language explanation series - array explanation, one-dimensional array, two-dimensional array

聊一聊数据库的行存与列存

对spark算子aggregateByKey的理解

YOLO系列损失函数详解

Learn software testing in two weeks? I was shocked!
随机推荐
EMC中class A和class B哪个更严格?
Niuke MySQL - SQL must know and know
leetcode 字符串类
Forward propagation of deep learning neural networks (1)
铜铟硫CuInSe2量子点修饰DNA(脱氧核糖核酸)DNA-CuInSe2QDs(齐岳)
解析树形结构 js
2022年湖南工学院ACM集训第五次周测AD题题解
flowable工作流所有业务概念
Lecture notes a utility for everyone to generate PCG
Will ordinary browsers disclose information? How to protect privacy by using a secure browser?
【花书笔记】 之 Chapter01 引言
Synthesis of dna-ag2sqds DNA modified silver sulfide Ag2S quantum dots
Swm32 series tutorial 5-adc application
Which of class A and class B is more stringent in EMC?
XMPP Service Research (II) prosody create account
对spark算子aggregateByKey的理解
[Google] solve the problem that Google browser does not pop up the account and password save box and cannot save login information
Merge two sorted linked lists - two questions per day
【13】加法器:如何像搭乐高一样搭电路(上)?
Why is ESD protection so important for integrated circuits? How to protect?