QT线程独占
各位大虾,我做了个线程独占的测试程序,代码如下:
/******************定义两个线程thread1和thread2**************************/
#ifndef TEST_H
#define TEST_H
#include <qthread.h>
#include <iostream.h>
#include <qmutex.h>
#include <qwaitcondition.h>
class Thread1:public QThread{
public:
Thread1(QMutex *mutex_1, QWaitCondition *cond_1){};
~Thread1(){};
virtual run(){
for(int i=0;i <5;i++){
cout < < "thread1 ping " < <endl;
sleep(1);
}
};
};
class Thread2:public QThread{
public:
Thread2(QMutex *mutex_2, QWaitCondition *cond_2){};
~Thread2(){};
virtual run(){
for(int i=0;i <5;i++){
cout < < "------thread2 ping---- " < <endl;
sleep(2);
}
};
};
#endif
/*****************主程序如下********************/
#include "test.h "
int main(int argc, char *argv[])
{
QMutex mutex;
QWaitCondition condition;
Thread1 *thread1;
Thtead2 *thread2;
mutex.lock();
thread1=new Thread1(&mutex, &condition);
thread1-> start();
mutex.unlock();
mutex.lock();
thread2=new Thread2(&mutex,&condition);
thread2-> start();
mutex.unlock();
return 0;
}
可是出现的是如下的结果;
thread1 ping
thread1 ping
------thread2 ping----
thread1 ping
thr