本人小白刚学,菜菜,喷轻点
创建4个线程分别对应4个人,使用4个信号量对应盘子panzi(初始化为1),当前抢夺到的线程mutex(初始化为1),苹果apple(初始化为0),桔子orange(初始化为0)
ui界面:
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
(1)widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QThread>
#include <QSemaphore>
#include "father.h"
#include "mother.h"
#include "daughter.h"
#include "son.h"
#include <QDebug>
extern QSemaphore empty;//盘子是否为空
extern QSemaphore mutex;//四个人对盘子的互斥访问
extern QSemaphore orange;//是否可以取桔子
extern QSemaphore apple;//是否可以取苹果
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
//四个人的不同操作
void fatherTake();
void motherTake();
void sonTake();
void daughterTake();
private slots:
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
QSemaphore empty(1);
QSemaphore mutex(1);
QSemaphore apple(0);
QSemaphore orange(0);
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pictureLab->setPixmap(QPixmap(":/new/prefix1/C:/picture/konglanzi.jpg").scaled(200,200));
ui->lineEdit->setText("空盘子,请等待");
//创建父亲线程对象
father *father1=new father;
//点击父亲的操作,对应应该创建一个线程,来处理对应的操作
//父亲线程来来处理信号量,信号槽中来处理或信号槽中添加处理主界面的函数
connect(father1,&father::number,this,[=](int num){
if(num==1)
{
int a=empty.available();
int b=mutex.available();
int c=orange.available();
int d=apple.available();
ui->lineEdit->setText("父亲放入一个苹果");
ui->pictureLab->setPixmap(QPixmap(":/new/prefix1/C:/picture/apple.jpg").scaled(200,200));
qDebug("a:%d,b:%d,c:%d,d:%d",a,b,c,d);
}
else
{
ui->lineEdit->setText("盘子已被占用");
}
});
connect(ui->fatherButt,&QPushButton::clicked,this,[=](){
father1->start();
});
mother *mother1=new mother;
connect(mother1,&mother::number,this,[=](int num){
if(num==1)
{
int a=empty.available();
int b=mutex.available();
int c=orange.available();
int d=apple.available();
ui->lineEdit->setText("母亲放入一个桔子");
ui->pictureLab->setPixmap(QPixmap(":/new/prefix1/C:/picture/orange1.jpg").scaled(200,200));
qDebug("a:%d,b:%d,c:%d,d:%d",a,b,c,d);
}
else
{
ui->lineEdit->setText("盘子已被占用");
}
});
connect(ui->motherButt,&QPushButton::clicked,this,[=](){
mother1->start();
});
daughter *daughter1=new daughter;
connect(daughter1,&daughter::number,this,[=](int num){
if(num==1)
{
int a=empty.available();
int b=mutex.available();
int c=orange.available();
int d=apple.available();
ui->lineEdit->setText("女儿吃掉一个苹果");
ui->pictureLab->setPixmap(QPixmap(":/new/prefix1/C:/picture/konglanzi.jpg").scaled(200,200));
qDebug("a:%d,b:%d,c:%d,d:%d",a,b,c,d);
}
else if(num==2)
{
ui->lineEdit->setText("盘子中水果为桔子,女儿不吃");
}
else
{
ui->lineEdit->setText("盘子中水果为空");
}
});
connect(ui->daughterButt,&QPushButton::clicked,this,[=](){
daughter1->start();
});
son *son1=new son;
connect(son1,&son::number,this,[=](int num){
if(num==1)
{
int a=empty.available();
int b=mutex.available();
int c=orange.available();
int d=apple.available();
ui->lineEdit->setText("儿子吃掉一个桔子");
ui->pictureLab->setPixmap(QPixmap(":/new/prefix1/C:/picture/konglanzi.jpg").scaled(200,200));
qDebug("a:%d,b:%d,c:%d,d:%d",a,b,c,d);
}
else if(num==2)
{
ui->lineEdit->setText("盘子中水果为苹果,儿子不吃");
}
else
{
ui->lineEdit->setText("盘子中水果为空");
}
});
connect(ui->sonButt,&QPushButton::clicked,this,[=](){
son1->start();
});
}
Widget::~Widget()
{
delete ui;
}
(2)father.h
#ifndef FATHER_H
#define FATHER_H
#include <QThread>
#include <QSemaphore>
extern QSemaphore empty;
extern QSemaphore mutex;
extern QSemaphore apple;
extern QSemaphore orange;
class father : public QThread
{
Q_OBJECT
public:
explicit father(QObject *parent = nullptr);
protected:
//创建的子线程处理的任务,需要写到run中
//这里处理对应的信号量操作,放置苹果的操作
void run();
signals:
//自定义信号
void number(int num);
};
#endif // FATHER_H
father.cpp
#include "father.h"
father::father(QObject *parent) : QThread(parent)
{
}
void father::run()
{
if(empty.tryAcquire()&&mutex.tryAcquire())
{
//子线程向主线程发送命令,在主线程窗口中实现操作
emit number(1);
apple.release();
mutex.release();
}
else
{
emit number(0);
}
}
(3)mother.h
#ifndef MOTHER_H
#define MOTHER_H
#include <QThread>
#include <QSemaphore>
extern QSemaphore empty;
extern QSemaphore mutex;
extern QSemaphore apple;
extern QSemaphore orange;
class mother : public QThread
{
Q_OBJECT
public:
explicit mother(QObject *parent = nullptr);
protected:
//创建的子线程处理的任务,需要写到run中
//这里处理对应的信号量操作,放置苹果的操作
void run();
signals:
//自定义信号
void number(int num);
};
#endif // MOTHER_H
mother.cpp
#include "mother.h"
mother::mother(QObject *parent) : QThread(parent)
{
}
void mother::run()
{
if(empty.tryAcquire()&&mutex.tryAcquire())
{
//成功拿到盘子和互斥量,发送信号
emit number(1);
orange.release();
mutex.release();
}
else
{
emit number(0);
}
}
(3)son.h
#ifndef SON_H
#define SON_H
#include <QThread>
#include <QSemaphore>
extern QSemaphore empty;
extern QSemaphore mutex;
extern QSemaphore apple;
extern QSemaphore orange;
class son : public QThread
{
Q_OBJECT
public:
explicit son(QObject *parent = nullptr);
protected:
//创建的子线程处理的任务,需要写到run中
//这里处理对应的信号量操作,放置苹果的操作
void run();
signals:
//自定义信号
void number(int num);
};
#endif // SON_H
son.cpp
#include "son.h"
son::son(QObject *parent) : QThread(parent)
{
}
void son::run()
{
if(orange.tryAcquire()&&mutex.tryAcquire())
{
//成功,发送信号
emit number(1);
empty.release();
mutex.release();
}
else if(apple.tryAcquire()&&mutex.tryAcquire())
{
emit number(2);
apple.release();
mutex.release();
}
else
{
emit number(0);
}
}
(4)daughter.h
#ifndef DAUGHTER_H
#define DAUGHTER_H
#include <QThread>
#include <QSemaphore>
extern QSemaphore empty;
extern QSemaphore mutex;
extern QSemaphore apple;
extern QSemaphore orange;
class daughter : public QThread
{
Q_OBJECT
public:
explicit daughter(QObject *parent = nullptr);
protected:
//创建的子线程处理的任务,需要写到run中
//这里处理对应的信号量操作,放置苹果的操作
void run();
signals:
//自定义信号
void number(int num);
};
#endif // DAUGHTER_H
daughter.cpp
#include "daughter.h"
daughter::daughter(QObject *parent) : QThread(parent)
{
}
void daughter::run()
{
if(apple.tryAcquire()&&mutex.tryAcquire())
{
//成功,发送信号
emit number(1);
empty.release();
mutex.release();
}
else if(orange.tryAcquire()&&mutex.tryAcquire())
{
emit number(2);
orange.release();
mutex.release();
}
else
{
emit number(0);
}
}
运行截图: