简体   繁体   English

QThread:如何使用受保护的静态函数

[英]QThread:How to use protected static functions

I learn from the following links that sub classing a QThread is not a correct way of using it...the proper way is to subclass the QObject and then move the object of QObject class to the respective thread using moveToThread() function...i followed the following links.. link 1 and link 2 ...but my question is then how will i be able to use msleep() and usleep() protected static functions ? 我从以下链接中了解到,对QThread进行子类化并不是使用它的正确方法......正确的方法是将QObject子类化,然后使用moveToThread()函数将QObject类的对象移动到相应的线程...我按照以下链接.. 链接1链接2 ...但我的问题是,我将如何能够使用msleep()和usleep()保护静态函数? or will i use QTimer to make a thread wait for some time ? 或者我会使用QTimer使线程等待一段时间?

No need for timers. 不需要计时器。 For waiting, Qt provides QWaitCondition. 等待,Qt提供QWaitCondition。 You can implement something like this: 你可以实现这样的事情:

#include <QWaitCondition>
#include <QMutex>

void threadSleep(unsigned long ms)
{
    QMutex mutex;
    mutex.lock();
    QWaitCondition waitCond;
    waitCond.wait(&mutex, ms); 
    mutex.unlock();
}

This is a normal function. 这是正常的功能。 You can of course implement it as a member function too, if you want (it can be a static member in that case.) 如果你愿意的话,你当然可以将它实现为成员函数(在这种情况下它可以是static成员。)

One solution would be to create a timer: 一种解决方案是创建一个计时器:

class Worker: public QObject
{
///...

private slots:
void doWork()
{
    //...
    QTimer::singleShot(delay, this, SLOT(continueDoingWork()));
}

void continueDoingWork()
{
}
};

Sometimes, you only need to run an operation in a different thread and all this event loops and threads are overhead. 有时,您只需要在不同的线程中运行操作,并且所有这些事件循环和线程都是开销。 Then you can use QtConcurent framework: 然后你可以使用QtConcurent框架:

class Worker
{
public:
void doWork()
{
//...
}
} worker;

//...

QtConcurent::run(worker, &Worker::doWork);

Then, I usually use mutexes to simulate the sleep operations: 然后,我通常使用互斥锁来模拟睡眠操作:

QMutex m;
m.lock();
m.tryLock(delay);

The canonical answer is "use signals and slots." 规范的答案是“使用信号和插槽”。

For example, if you want a QObject in a thread to "wake itself up" after a certain period of time, consider QTimer::singleShot() , passing in the slot as the third argument. 例如,如果您希望线程中的QObject在一段时间后“唤醒自己”,请考虑QTimer :: singleShot() ,将插槽作为第三个参数传入。 This can be called from the slot in question, resulting in periodic execution. 这可以从有问题的槽中调用,从而导致定期执行。

You can't let a different thread sleep without cooperation of this thread, thats the reason the member functions of QThread are protected. 如果没有这个线程的合作,你不能让一个不同的线程睡眠,这就是QThread的成员函数受到保护的原因。 If you want to sleep a different thread you need to use a condition variable or a timer inside 如果你想睡一个不同的线程,你需要在里面使用一个条件变量或一个计时器

If you want so sleep the current thread with usleep() , the simplest way is to subclass it - its perfectly fine as long as you don't need QThreadPool , a thread local event loop or similar. 如果你想用usleep()睡觉当前线程,最简单的方法是将它子类化 - 只要你不需要QThreadPool ,一个线程本地事件循环或类似的东西就完全没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM