QT QSemaphore 线程同步

简述:
信号量的规则如下:
(1)如果当前资源计数大于0,那么信号量处于触发状态
(2) 如果当前资源计数等于0,那么信号量处于未触发状态
(3)系统绝对不会让当前资源计数变为负数
(4)当前资源计数绝对不会大于最大资源计数
——-摘自《Windows核心编程》
当然,QT这边没看到怎么设置(4)最大资源数。

QSemaphore 方法:

QSemaphore::​QSemaphore(int n = 0):构造函数
Creates a new semaphore and initializes the number of resources it guards to n (by default, 0).
创建信号和初始化它保护的资源数目

int QSemaphore::​available() const:
Returns the number of resources currently available to the semaphore. This number can never be negative.
返回当前信号量可用资源的数量。这个数字不能为负数。

void QSemaphore::​acquire(int n = 1):
Tries to acquire n resources guarded by the semaphore. If n > available(), this call will block until enough resources are available.
尝试获取n个被保护资源。如果n>available(),调用将被阻塞,直到有足够的资源可用–即n

应用场景:

1、线程A、线程B这2个线程,要求B满足特定条件,发射信号,在A中(槽)将信号内容打印到窗口。
2、使用信号量解决生产者与消费者问题

eg:
场景1、
B线程

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QThread>
#include <QSemaphore>

class MyThread : public QThread
{
    Q_OBJECT
public:
    MyThread();
    ~MyThread();
    void stop();

signals:
    void prin(const QString &);

protected:
    void run();
private:
    volatile bool stopped;

public:
    QSemaphore semaphore;
};

#endif // MYTHREAD_H
#include "mythread.h"
#include <QDebug>


MyThread::MyThread():semaphore(1)
{
    stopped=false;
}

MyThread::~MyThread()
{

}

void MyThread::stop()
{
    stopped=true;
}

void MyThread::run()
{
    qreal i=0;
    while(!stopped){
        //qDebug()<<QString("in MyThread:%1").arg(i++);
        semaphore.acquire();
        emit prin(QString(":%1").arg(i++));
        //usleep(1);
    }
    stopped=false;
}

A线程:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "mythread.h"
#include <QMutex>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private slots:
    void on_startButton_clicked();

    void on_stopButton_clicked();
    void showPrin(const QString&);

private:
    Ui::Dialog *ui;
    MyThread thread;
    QMutex mutex;
};

#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"
#include <QSemaphore>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    //connect(&thread,SIGNAL(prin(QString)),this,SLOT(showPrin(QString)),Qt::QueuedConnection);
    connect(&thread,SIGNAL(prin(const QString&)),this,SLOT(showPrin(const QString&)));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_startButton_clicked()
{
    thread.start();
    ui->startButton->setEnabled(false);
    ui->stopButton->setEnabled(true);
}

void Dialog::on_stopButton_clicked()
{
    if(thread.isRunning()){
        thread.stop();
        ui->startButton->setEnabled(true);
        ui->stopButton->setEnabled(false);
    }
}

void Dialog::showPrin(const QString &thread_id)
{
    ui->textBrowser->setText(thread_id);
    thread.semaphore.release();
}

效果:
这里写图片描述
显示线程循环次数。
留意:
semaphore(1)
线程B的 semaphore.acquire();
主线程A的 thread.semaphore.release();
ui->textBrowser->setText(thread_id);线程A打印完成, 线程B才能够继续,
如果 不使用信号量同步下或者sleep()延时调度 下,直接崩盘。。。

场景2、
详见:Qt Creator快速入门 同步线程章节。

#include <QtCore>
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
//#include "producer.h"
//#include "consumer.h"

const int DataSize = 10;
#define BufferSize  5
char buffer[BufferSize];
QSemaphore freeByte(BufferSize);
QSemaphore usedBytes;

class Producer : public QThread
{
public:
    Producer();
    ~Producer();
    void run();
};

Producer::Producer()
{

}

Producer::~Producer()
{

}

void Producer::run()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i=0;i<DataSize;++i){
        //qDebug()<<"aaa"<<freeByte.available()<<usedBytes.available();
        freeByte.acquire();
        buffer[i%BufferSize]="ACGT"[(int)qrand()%4];
        qDebug()<<QString("producer:%1--------producer%2").arg(buffer[i%BufferSize]).arg(i);
        usedBytes.release();
        //qDebug()<<"bbb"<<freeByte.available()<<usedBytes.available();
    }
    //exec();
}

//
class Consumer : public QThread
{
public:
    Consumer();
    ~Consumer();
    void run();
};

Consumer::Consumer()
{

}

Consumer::~Consumer()
{

}

void Consumer::run()
{
    for(int i=0;i<DataSize;i++){
        usedBytes.acquire();
        qDebug()<<QString("consumer:%1--------consumer%2").arg(buffer[i%BufferSize]).arg(i);
        freeByte.release();
    }
    //exec();
}

int main(int argc,char *argv[])
{
    QCoreApplication app(argc,argv);
    Producer producer;
    Consumer consumer;
    producer.start();
    consumer.start();
    producer.wait();
    consumer.wait();
    return app.exec();
}

留意:
这里写图片描述
这里写图片描述


注:

semaphore(1)
semaphore(n)—n>1
应用于不同的场景。。。
以上,留存….

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值