Qt下如何控制线程的执行顺序?
大家好,有2个函数分别封装了两个线程,ThreadA()和ThreadB(),我想在ThreadA执行完后执行ThreadB,应该怎么实现?不使用信号的槽的机制,
想通过线程句柄来判断。如:
rc = ThreadA();
if(rc)
ThreadB() --------------------编程问答-------------------- 可以使用信号量 --------------------编程问答--------------------
麻烦你可不能再详细一点。。。 --------------------编程问答-------------------- #include <QApplication>
#include <QThread>
#include <QDebug>
#include <QMutex>
#include <QMutexLocker>
#include <QReadWriteLock>
#include <QSemaphore>
QString strGlobal("Global String");
QSemaphore sem(1);
class ReadThread : public QThread
{
public:
void run()
{
sem.acquire(1);
qDebug()<<strGlobal;
sem.release(1);
}
};
class WriteThread : public QThread
{
public:
void run()
{
sem.acquire(1);
strGlobal="write form write thread";
qDebug()<<strGlobal;
sem.release(1);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ReadThread readthread;
readthread.start();
WriteThread writethread;
writethread.start();
readthread.wait();
writethread.wait();
return a.exec();
}
我用QSemaphore 试了下,可执行结果不固定
有时候是这样的结果:
"Global String"
"write form write thread"
有时候是这样的结果:
"write form write thread"
"write form write thread"
我只想要第一种结果,不想要其它的结果 --------------------编程问答-------------------- 用互斥量QMutex不可以吗
补充:移动开发 , Qt