Qt 线程
来源:互联网 发布:iphone越狱定位软件 编辑:程序博客网 时间:2024/06/06 00:32
Qt中的线程是与平台无关的
QThread 提供了创建一个新线程的方法
新建一个线程,继承QThread并重写它的run()当调用 start()函数时会调用重载的run()函数
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>class MyThread : public QThread{ Q_OBJECTpublic: bool stop ; explicit MyThread(QObject *parent = 0); void run();signals:public slots:};#endif // MYTHREAD_H
#include "mythread.h"#include<QDebug>MyThread::MyThread(QObject *parent) : QThread(parent){ stop = false;}void MyThread::run(){ for(int i=0;i<1000;i++) { if(stop)break; qDebug()<<i; QThread::sleep(1); }}
#include <QCoreApplication>#include "myobject.h"#include <QThread>#include<QDebug>#include "mythread.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); MyThread myThread; myThread.start(); QThread::sleep(10); myThread.stop=true; return a.exec();}
看一下Qt中包含的线程类:
QThread 所有线程的基类,提供了创建一个线程的方法
QThreadStorge 提供一逐线程数据存储
QMutex 提供相互排斥的锁,或互斥量
QMutexLocker 可以自动对QMutex加锁与解锁
QReadWirterLock 提供了一个可以同时读操作的锁
QreadLocker与QwriteLocker可以自动对QReadWriteLock加锁与解锁
QSempHore提供了一个整形信号量,是互斥的泛化
QWaitCondition提供了一种方法,使线程可以在被另外线程唤醒之前一直休眠
线程的同步
QMutex 提供相互排斥的锁,或互斥量
QMetex提供了lock和Unlock函数,如果 一个已经锁定 这个互斥量,只有这个线程unlock后其它线程才可以
访问
把上边的例子做一下修改,只是为了方便大家理解,这个例子没什么作用
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>class QMutex;class MyThread : public QThread{ Q_OBJECTprivate: QMutex qm; bool stop;public: explicit MyThread(QObject *parent = 0); void run(); void SetFlg(bool flg);signals:public slots:};#endif // MYTHREAD_H
#include "mythread.h"#include<QDebug>#include<QMutex>MyThread::MyThread(QObject *parent) : QThread(parent){ stop = false;}void MyThread::SetFlg(bool flg){ qm.lock(); stop=flg; qm.unlock();}void MyThread::run(){ for(int i=0;i<1000;i++) { qm.lock(); if(stop) { qm.unlock(); break; } qDebug()<<i; QThread::sleep(1); qm.unlock(); }}
#include <QCoreApplication>#include "myobject.h"#include <QThread>#include<QDebug>#include "mythread.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); MyThread myThread; myThread.start(); QThread::sleep(10); myThread.SetFlg(true); return a.exec();}
使用QMutex时要小心因为如果 lock()后没有unlock()会导致死锁别的线程就
永远也不能访问资源了
Qt提供了QMutexLocker来解决这个问题
修改我们的app文件
#include "mythread.h"#include<QDebug>#include<QMutex>#include<QMutexLocker>MyThread::MyThread(QObject *parent) : QThread(parent){ stop = false;}void MyThread::SetFlg(bool flg){ QMutexLocker locker(&qm); stop=flg;}void MyThread::run(){ QMutexLocker locker(&qm); for(int i=0;i<1000;i++) { if(stop) { break; } qDebug()<<i; QThread::sleep(1); }}
QMutexLocker会自己unluck
QMutexLocker也提供了一个mutex()成员函数返回QMutexLocker操作的互斥量。对于需要访问互斥量是十分有用的,比如QWaitCondition::wait()。
QReadWirterLock
用mutext进行线程同步有一个问题某个时刻只许一个线程访问资源如果同时有多个线程对共享资源进行访问,
同时有写操作线程那么这种情况下采用mutex就会成为程序的瓶颈。使用QReadWriteLock来实现多线程
读操作,一个线程写操作,写线程执行时会阻塞所有的读线程,而读线程运行不需要进行同步
QReadWriteLock lock;void ReaderThread::run(){ ... lock.lockForRead(); read_file(); lock.unlock(); ...}void WriterThread::run(){ ... lock.lockForWrite(); write_file(); lock.unlock(); ...}
QreadLocker和QWriteLocker类是对QReadWirterLock 的简化
它们会自动unluck();
QReadWriteLock lock;QByteArray readData(){ QReadLocker locker(&lock); ... return data;}
相当于QReadWirterLock 的写法如下
QReadWriteLock lock;QByteArray readData(){ lock.lockForRead(); ... lock.unlock(); return data;}
- QT线程
- Qt 线程
- Qt线程
- Qt线程
- QT线程
- Qt 线程
- QT 线程
- qt线程
- 界面、线程与Qt线程
- Qt中的线程支持
- Qt中的线程支持
- qt线程类---QThread
- QT 线程的使用
- QT---线程间通信
- Qt中的线程支持
- Qt之多线程(一)
- Qt之多线程(二)
- QT中的线程
- redis主从复制问题
- 创建属于你的SDK!
- Redis学习(二)redis配置
- C#Thread的使用
- CocoaPods安装及错误问题
- Qt 线程
- java-Java可以对方法进行序列化?
- 南阳理工ACM 50爱摘苹果的小明
- jQuery中获取radio button中被选中的value值
- 欢迎使用CSDN-markdown编辑器
- stl中list的sort算法实现
- 怎么在eclipse中建立spring bean file
- 动态指定select option selected选中值
- C++ 获取文件夹下的所有文件名