• QT(41)-多线程-QTThread-同步QSemaphore-互斥QMutex


    1.QTThread
    2.QSemaphore
    3.QMutex


    1.QTThread
     

     

     1.1QThread h 头文件

    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3. #include
    4. #1.继承于public QThread
    5. //2.虚函数run()
    6. class MyThread:public QThread
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MyThread(QObkect *parent=0);
    11. protected:
    12. virtual void run();
    13. signals:
    14. public slots:
    15. };
    16. #endif

    1.2.QThread cpp文件

    1. #include"MyThread.h"
    2. #include
    3. MyThread::MyThread(QObject *parent):QThtrad(parent)
    4. {
    5. }
    6. void MyThread::run()
    7. {
    8. int i=1;
    9. for(i=1;i<=5;i++)
    10. {
    11. qDebug()<
    12. sleep(1);
    13. }
    14. }


        1.3 main 调用文件

    1. #include
    2. #include"MainWindow.h"
    3. #include"MyThread.h"
    4. int main(int argc,char *argv[])
    5. {
    6. QApplication a(argc,argv);
    7. MyThread mt;
    8. mt.start();
    9. MainWindow mw;
    10. mw.show();
    11. return a.exec();
    12. }

      2.同步QSemaphore

     

    线程的执行顺序是由信号量的个数指定的。

     

     两个线程类
    ThreadNum tn;          //打印1 2 3 
    ThreadAlphabet ta;   //打印 abc
    先打印 abc 再打印 1 2 3

    1. #include
    2. #include"ThreadNum.h"
    3. #include"ThreadAlphabet.h"
    4. int main(int argc,char *argv[])
    5. {
    6. QApplication a(argc,argv);
    7. ThreadNum tn;
    8. ThreadAlphabet ta;
    9. tn.start();
    10. ta.start();
    11. return a;
    12. }

    //semManager 单例的模式

    1. #include
    2. class SemManager
    3. {
    4. //1.创建两个变量
    5. QSemaphore semnum; // 对应于ThreadNum
    6. QSemaphore semAlphabet;// 对应于ThreadAlphabet
    7. //2.私有化单例
    8. public:
    9. static SemManager *getInstance();
    10. private:
    11. static SemManager *instance;
    12. SemManager();
    13. };
    14. #endif

    semManager.cpp

    1. #include "SemManager.h"
    2. SemManager *SemManager::instance=0;
    3. SemManager *SemManager::getInstance()
    4. {
    5. if(0==instance)
    6. instance=new SemManager;
    7. return instance;
    8. }
    9. SemManager::SemManager():semnum(0),semAlphabet(1)
    10. {
    11. }

    .acquire() -1      release() +1
    2.1ThreadAlphabet 调用

    1. #include"ThreadAlphabet.h"
    2. #include
    3. #include"SemManager.h"
    4. ThreadAlphabet::ThreadAlphabet(QObject *parent):QThtread(parent)
    5. {
    6. }
    7. void ThreadAlphabet::run()
    8. {
    9. int i=0;
    10. SemManager *sm= SemManager ::getInstance();
    11. for(i=0;i<=3;i++) //初始值为0
    12. {
    13. sm->semAlphabet.acquire(); //semAlphabet -1
    14. qDebug()<<(char)('a'+i);
    15. sleep(2);
    16. sm->semNum.release(); //semNum +1
    17. }
    18. }

    2.2ThreadNum调用

    1. #include"ThreadNum.h"
    2. #include
    3. #include"SemManager.h"
    4. ThreadNum::ThreadNum(QObject *parent):QThtread(parent)
    5. {
    6. }
    7. void ThreadNum::run()
    8. {
    9. int i=0;
    10. SemManager *sm= SemManager ::getInstance();
    11. for(i=1;i<=3;i++) //初始值为1
    12. {
    13. sm->semNum.acquire(); //semNum -1
    14. qDebug()<
    15. sleep(1);
    16. sm->semAlphabet.release(); //semAlphabet +1
    17. }
    18. }

    3.QMutex

    Ticket.h

    1. #include
    2. #include //1.添加头文件
    3. class Ticket
    4. {
    5. private:
    6. int ticketCount;
    7. QMutex mutex; //2.添加变量
    8. public:
    9. Ticket(int count=1);
    10. void takeTicket();
    11. };
    12. class TicketThread:public QThread //线程函数 对数量进行减操作
    13. {
    14. Q_OBJECT
    15. public:
    16. explicit TicketThread(QObject *parent=0);
    17. protected:
    18. virtual void run();
    19. }


    Ticket.cpp

    1. #include"TicketThread.h"
    2. #include
    3. Ticket ticket(1);
    4. //1.Ticket
    5. Ticket::Ticket(int count):ticketCount(count)
    6. {
    7. }
    8. void Ticket::takeTicket()
    9. {
    10. mutex.lock(); //1.函数上锁
    11. if(ticketCount >0)
    12. {
    13. ticketCount--;
    14. qDebug()<<"take success,last:"<
    15. }
    16. else
    17. {
    18. qDebug()<<"take failed";
    19. }
    20. mutex.unlock(); //2.函数解锁
    21. }
    22. //2.TicketThread
    23. TicketThread::TicketThread(QObject *parent):QThread(parent)
    24. {
    25. }
    26. void TicketThread::run()
    27. {
    28. ticket.takeTicket();
    29. }

    main.cpp
     

    1. #include
    2. #include "mainwindow.h"
    3. #include "TicketThread.h"
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc,argv);
    7. TicketThread tt1;
    8. TicketThread tt2;
    9. TicketThread tt3;
    10. TicketThread tt4;
    11. tt1.start();
    12. tt2.start();
    13. tt3.start();
    14. tt4.start();
    15. return a.exec();
    16. }

     应该不会出现负数,但是实际上出现了。多线程时会让判断实效的。

     

  • 相关阅读:
    pytorch的searchsorted解释
    Java版企业电子招标采购系统源码—企业战略布局下的采购寻源
    SUBMIT指定用户名错误
    【25】c++设计模式——>责任链模式
    服务器怎么关闭防火墙
    如何开展性能测试,你知道吗?
    【论文阅读】MDK与MILDM
    22-08-01 西安 尚医通(01)跨域配置、Swagger2、R类、统一异常处理和自定义异常、Logback日志
    小程序真机调试提示系统错误
    DC电源模块选用电容滤波器的注意事项
  • 原文地址:https://blog.csdn.net/aggie4628/article/details/126133160