Qt---多執行緒控制

諦聽-發表於2016-07-05

1、QMutex

#include <QMutex>

class Key
{
private:
    int key;
    QMutex mutex;

public:
    Key()
    {
        key = 0;
    }

    int creatKey()
    {
        mutex.lock();
        ++key;
        return key;
        mutex.unlock();
    }

    int value() const
    {
        mutex.lock();
        return key;
        mutex.unlock();
    }
};

2、QMutexLocker

#include <QMutex>
#include <QMutexLocker>

class Key
{
private:
    int key;
    QMutex mutex;

public:
    Key()
    {
        key = 0;
    }

    int creatKey()
    {
        QMutexLocker.locker(&mutex);
        ++key;
        return key;
    }

    int value() const
    {
        QMutexLocker.locker(&mutex);
        return key;
    }

};

3、QSemaphore

.pro中的更改

QT       += core
QT       -= gui

CONFIG   += console
CONFIG   -= app_bundle

main.cpp

#include <QCoreApplication>
#include <QSemaphore>
#include <QThread>
#include <stdio.h>

const int DataSize = 1000;
const int BufferSize = 80;
int buffer[BufferSize];

QSemaphore freeBytes(BufferSize);  //空閒位元組的數量
QSemaphore usedBytes(0);  //被資料佔用位元組的數量

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

Producer::Producer()
{

}

void Producer::run()
{
    for(int i=0; i<DataSize; i++)
    {
        freeBytes.acquire();  //如果空閒位元組數為0,生產者睡眠;否則,生產者生產資料
        buffer[i%BufferSize] = (i%BufferSize);
        usedBytes.release();  //喚醒消費者
    }
}

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

Consumer::Consumer()
{

}

void Consumer::run()
{
    for(int i=0; i<DataSize; i++)
    {
        usedBytes.acquire();   //如果沒有資料,消費者睡眠;否則,消費者消耗資料
        fprintf(stderr, "%d", buffer[i%BufferSize]);
        if(i % 16 == 0 && i != 0)
            fprintf(stderr, "\n");
        freeBytes.release();   //喚醒生產者
    }
    fprintf(stderr, "\n");
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Producer producer;
    Consumer consumer;

    producer.start();
    consumer.start();

    producer.wait();
    consumer.wait();

    return a.exec();
}

這裡寫圖片描述

4、執行緒等待與喚醒
.pro中的更改

QT       += core
QT       -= gui

CONFIG   += console
CONFIG   -= app_bundle

main.cpp

#include <QCoreApplication>
#include <QWaitCondition>
#include <QMutex>
#include <QThread>
#include <stdio.h>

const int DataSize = 1000;
const int BufferSize = 80;
int buffer[BufferSize];

QWaitCondition bufferEmpty;
QWaitCondition bufferFull;

QMutex mutex;
int numUsedBytes = 0;
int rIndex = 0;

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

Producer::Producer()
{

}

void Producer::run()
{
    for(int i=0; i<DataSize; i++)
    {
        mutex.lock();

        if(numUsedBytes == BufferSize) //如果緩衝區已滿,則生產者睡眠
            bufferEmpty.wait(&mutex);

        buffer[i%BufferSize] = numUsedBytes;
        ++numUsedBytes;
        bufferFull.wakeAll();  //喚醒消費者

        mutex.unlock();
    }
}

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

Consumer::Consumer()
{

}

void Consumer::run()
{
    forever
    {
        mutex.lock();

        if(numUsedBytes == 0)  //如果緩衝區空,消費者睡眠
            bufferFull.wait(&mutex);

        printf("%ul::[%d]= %d\n", currentThreadId(), rIndex, buffer[rIndex]);
        rIndex = (++rIndex) % BufferSize;
        --numUsedBytes;
        bufferEmpty.wakeAll(); //喚醒生產者

        mutex.unlock();
    }
    printf("\n");
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Producer producer;
    Consumer consumerA;
    Consumer consumerB;

    producer.start();
    consumerA.start();
    consumerB.start();

    producer.wait();
    consumerA.wait();
    consumerB.wait();

    return a.exec();
}

這裡寫圖片描述

相關文章