1.QTThread
2.QSemaphore
3.QMutex
1.QTThread
1.1QThread h 头文件
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
-
- #include
-
- #1.继承于public QThread
- //2.虚函数run()
- class MyThread:public QThread
- {
- Q_OBJECT
- public:
- explicit MyThread(QObkect *parent=0);
- protected:
- virtual void run();
- signals:
- public slots:
-
- };
-
- #endif
1.2.QThread cpp文件
- #include"MyThread.h"
- #include
- MyThread::MyThread(QObject *parent):QThtrad(parent)
- {
- }
- void MyThread::run()
- {
- int i=1;
- for(i=1;i<=5;i++)
- {
- qDebug()<
- sleep(1);
- }
- }
1.3 main 调用文件
- #include
- #include"MainWindow.h"
- #include"MyThread.h"
- int main(int argc,char *argv[])
- {
- QApplication a(argc,argv);
- MyThread mt;
- mt.start();
-
- MainWindow mw;
- mw.show();
- return a.exec();
- }
2.同步QSemaphore
线程的执行顺序是由信号量的个数指定的。
两个线程类
ThreadNum tn; //打印1 2 3
ThreadAlphabet ta; //打印 abc
先打印 abc 再打印 1 2 3
- #include
- #include"ThreadNum.h"
- #include"ThreadAlphabet.h"
-
- int main(int argc,char *argv[])
- {
- QApplication a(argc,argv);
- ThreadNum tn;
- ThreadAlphabet ta;
-
- tn.start();
- ta.start();
- return a;
-
- }
//semManager 单例的模式
-
- #include
- class SemManager
- {
- //1.创建两个变量
- QSemaphore semnum; // 对应于ThreadNum
- QSemaphore semAlphabet;// 对应于ThreadAlphabet
- //2.私有化单例
- public:
- static SemManager *getInstance();
- private:
- static SemManager *instance;
- SemManager();
- };
- #endif
semManager.cpp
- #include "SemManager.h"
- SemManager *SemManager::instance=0;
- SemManager *SemManager::getInstance()
- {
- if(0==instance)
- instance=new SemManager;
- return instance;
- }
- SemManager::SemManager():semnum(0),semAlphabet(1)
- {
-
- }
.acquire() -1 release() +1
2.1ThreadAlphabet 调用
- #include"ThreadAlphabet.h"
- #include
- #include"SemManager.h"
- ThreadAlphabet::ThreadAlphabet(QObject *parent):QThtread(parent)
- {
- }
- void ThreadAlphabet::run()
- {
- int i=0;
- SemManager *sm= SemManager ::getInstance();
- for(i=0;i<=3;i++) //初始值为0
- {
- sm->semAlphabet.acquire(); //semAlphabet -1
- qDebug()<<(char)('a'+i);
- sleep(2);
- sm->semNum.release(); //semNum +1
- }
- }
2.2ThreadNum调用
- #include"ThreadNum.h"
- #include
- #include"SemManager.h"
- ThreadNum::ThreadNum(QObject *parent):QThtread(parent)
- {
- }
- void ThreadNum::run()
- {
- int i=0;
- SemManager *sm= SemManager ::getInstance();
- for(i=1;i<=3;i++) //初始值为1
- {
- sm->semNum.acquire(); //semNum -1
- qDebug()<
- sleep(1);
- sm->semAlphabet.release(); //semAlphabet +1
- }
- }
3.QMutex
Ticket.h
- #include
- #include
//1.添加头文件 - class Ticket
- {
- private:
- int ticketCount;
- QMutex mutex; //2.添加变量
- public:
- Ticket(int count=1);
- void takeTicket();
- };
-
- class TicketThread:public QThread //线程函数 对数量进行减操作
- {
- Q_OBJECT
- public:
- explicit TicketThread(QObject *parent=0);
- protected:
- virtual void run();
-
- }
Ticket.cpp
- #include"TicketThread.h"
- #include
- Ticket ticket(1);
- //1.Ticket
- Ticket::Ticket(int count):ticketCount(count)
- {
- }
-
- void Ticket::takeTicket()
- {
- mutex.lock(); //1.函数上锁
- if(ticketCount >0)
- {
- ticketCount--;
- qDebug()<<"take success,last:"<
- }
- else
- {
- qDebug()<<"take failed";
- }
-
- mutex.unlock(); //2.函数解锁
- }
-
-
- //2.TicketThread
- TicketThread::TicketThread(QObject *parent):QThread(parent)
- {
- }
- void TicketThread::run()
- {
- ticket.takeTicket();
- }
main.cpp
- #include
- #include "mainwindow.h"
- #include "TicketThread.h"
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc,argv);
-
- TicketThread tt1;
- TicketThread tt2;
- TicketThread tt3;
- TicketThread tt4;
-
- tt1.start();
- tt2.start();
- tt3.start();
- tt4.start();
-
- return a.exec();
-
- }
应该不会出现负数,但是实际上出现了。多线程时会让判断实效的。
-
相关阅读:
pytorch的searchsorted解释
Java版企业电子招标采购系统源码—企业战略布局下的采购寻源
SUBMIT指定用户名错误
【25】c++设计模式——>责任链模式
服务器怎么关闭防火墙
如何开展性能测试,你知道吗?
【论文阅读】MDK与MILDM
22-08-01 西安 尚医通(01)跨域配置、Swagger2、R类、统一异常处理和自定义异常、Logback日志
小程序真机调试提示系统错误
DC电源模块选用电容滤波器的注意事项
-
原文地址:https://blog.csdn.net/aggie4628/article/details/126133160