当前位置:网站首页>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 》
边栏推荐
- How to analyze the taxi business problem of didi SQL interview question
- node(一)
- Mysql, how many columns can be used to create an index?
- DNA modified osmium OS nanoparticles osnps DNA modified iridium nanoparticles irnps DNA
- XMPP Service Research (II) prosody create account
- 深度学习基础宝典---激活函数、Batch Size、归一化
- “蔚来杯“2022牛客暑期多校训练营2补题记录(DGHJKL)
- DNA脱氧核糖核酸修饰金属铂纳米颗粒PtNPS-DNA|科研试剂
- DNA deoxyribonucleic acid modified platinum nanoparticles ptnps DNA | scientific research reagent
- ArcGIS JS map internal and external network environment judgment
猜你喜欢

DNA修饰贵金属纳米颗粒|DNA脱氧核糖核酸修饰金属钯Pd纳米颗粒PdNPS-DNA

Google and Stanford jointly issued a document: why do we have to use large models?

EMC中class A和class B哪个更严格?

非关系型数据库之Redis【redis集群详细搭建】

铜铟硫CuInSe2量子点修饰DNA(脱氧核糖核酸)DNA-CuInSe2QDs(齐岳)

The underlying principles of RDB persistence and AOF persistence of redis

Use ffmpeg to generate single image + single audio streaming video in batches

Forward propagation of deep learning neural networks (1)

CLion调试redis6源码

Learn software testing in two weeks? I was shocked!
随机推荐
Freezing and thawing of pytoch
The underlying principles of RDB persistence and AOF persistence of redis
DNA-Ag2SQDs脱氧核糖核酸DNA修饰硫化银Ag2S量子点的合成方法
mysql,我们如何得到受查询影响的行数?
Which of class A and class B is more stringent in EMC?
DNA-CuInSeQDs近红外CuInSe量子点包裹脱氧核糖核酸DNA
华为高级工程师---BGP路由过滤及社团属性
DNA modified osmium OS nanoparticles osnps DNA modified iridium nanoparticles irnps DNA
EMC's "don't come back until you rectify"
2022年湖南工学院ACM集训第五次周测AD题题解
【17】 Establish data path (upper): instruction + operation =cpu
Lecture notes a utility for everyone to generate PCG
XMPP Service Research (II) prosody create account
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 supplementary question record (dghjkl)
任务管理器中,显示的CPU速度大于它的最大速度【主频】
[Google] solve the problem that Google browser does not pop up the account and password save box and cannot save login information
登录模式:单一服务器模式、单点登录、token模式
ArcGIS JS map internal and external network environment judgment
Mysql中有哪些不同的表格?
Information system project manager must recite the core examination site (41) risk management plan