QMutex QMutexLocker简单实例应用

来源:互联网 发布:腾讯足球数据统计 编辑:程序博客网 时间:2024/06/14 17:41

Qt help 上面的解释是:

QMutex类提供的是线程之间的访问顺序化。

QMutex的目的是保护一个对象、数据结构或者代码段,所以同一时间只有一个线程可以访问它。(在Java术语中,它和同步关键字“synchronized”很相似)。例如,这里有一个方法打印给用户两条消息:

  void someMethod()  {     qDebug("Hello");     qDebug("World");  }  

如果同时在两个线程中调用这个方法,结果的顺序将是:

  Hello  Hello  World  World  

如果你使用了一个互斥量:

  QMutex mutex;  void someMethod()  {     mutex.lock();     qDebug("Hello");     qDebug("World");     mutex.unlock();  }  

用Java的术语,这段代码应该是:

  void someMethod()  {     synchronized {       qDebug("Hello");       qDebug("World");     }  }  

然后同一时间只有一个线程可以运行someMethod并且消息的顺序也一直是正确的。当然,这只是一个很简单的例子,但是它适用于任何需要按特定频率发生的情况。

但你在一个线程中调用lock(),其它线程将会在同一地点试图调用lock()来阻塞,知道这个线程调用unlock()之后其它线程才会获得这个锁。lock()的一种非阻塞选择是tryLock()。

----------------------------------------------------------------------------------------------------------------------------------------------------------

实例1、:代码引用Qt多线程编程总结

#include "dialog.h"
#include <QApplication>
#include <Qthread>
#include <QTextStream>
#include <QMutex>
#include <QDebug>
#include <time.h>
#include <process.h>
#include "windows.h"
#include "winbase.h"
#include "winerror.h"
#include <iostream>
using namespace std;
class MyThreadA : public QThread {
public:
    virtual void run();
};
class MyThreadB: public QThread {
public:
    virtual void run();
};
QMutex mutex;
int number=6;
void MyThreadA::run(){
    qDebug()<<"111"<<endl;
    mutex.lock();
    number *= 5;
    sleep(1);
    number /= 4;
    mutex.unlock();
    qDebug()<<"22"<<endl;
}
void MyThreadB::run(){
     qDebug()<<"33"<<endl;
    mutex.lock();
    number *= 3;
    sleep(1);
    number /= 2;
    mutex.unlock();
    qDebug()<<"44"<<endl;
}
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyThreadA a;
    MyThreadB b;
    a.start();
    b.start();
//    a.wait();//为了等线程执行结束
//    b.wait();
     Sleep(5000);
     qDebug()<<QString::number(number,10)<<endl;
     return app.exec();
}
执行结果可以看到,线程a运行,线程B也几乎同时启动,但是线程B等待线程A结束释放lock才执行,实现保护全局变量number不会被线程b改变的作用。
实例2、
自动解锁机制QMutexLocker
  • QMutexLocker ( QMutex * mutex )
  • ~QMutexLocker ()
  • QMutex * mutex () const//Returns a pointer to the mutex that was locked in the constructor
  • void relock ()//Relocks an unlocked mutex locker
  • void unlock ()//Unlocks this mutex locker. You can use relock() to lock it again. It does not need to be locked when destroyed
我们学习QMutex可以知道对互斥量的操作很繁琐,并且出错了调试很麻烦,我们使用这个类就能很好的避免这个问题。
改造几句代码
#include "dialog.h"
#include <QApplication>
#include <Qthread>
#include <QTextStream>
#include <QMutex>
#include <QDebug>
#include <time.h>
#include <process.h>
#include "windows.h"
#include "winbase.h"
#include "winerror.h"
#include <iostream>
#include <QMutexLocker>
using namespace std;
class MyThreadA : public QThread {
public:
    virtual void run();
};
class MyThreadB: public QThread {
public:
    virtual void run();
};
QMutex mutex;
int number=6;
void MyThreadA::run(){
    qDebug()<<"111"<<endl;
//    mutex.lock();
    QMutexLocker locker(&mutex);
    number *= 5;
    sleep(1);
    number /= 4;
//    mutex.unlock();
    qDebug()<<"22"<<endl;
}
void MyThreadB::run(){
     qDebug()<<"33"<<endl;
 //   mutex.lock();
    QMutexLocker locker(&mutex);
    number *= 3;
    sleep(1);
    number /= 2;
 //   mutex.unlock();
    qDebug()<<"44"<<endl;
}
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyThreadA a;
    MyThreadB b;
    a.start();
    b.start();
//    a.wait();//为了等线程执行结束
//    b.wait();
     Sleep(5000);
     qDebug()<<QString::number(number,10)<<endl;
     return app.exec();
}
执行结果是一样的。因为QMutexLocker 申请的这个lock变量在这个函数退出时,自动的调用析构函数来解锁。