开发者

How can I protect a QThread function so it will not be called again until finished its previous work?

I'm using a QThread and inside its run method I have a timer invoking a function that performs some开发者_StackOverflow heavy actions that take some time. Usually more than the interval that triggers the timer (but not always).

What I need is to protect this method so it can be invoked only if it has completed its previous job.

Here is the code:

NotificationThread::NotificationThread(QObject *parent)
           : QThread(parent),
             bWorking(false),
             m_timerInterval(0)
{

}


NotificationThread::~NotificationThread()
{
    ;
}

void NotificationThread::fire()
{
    if (!bWorking)
    {
        m_mutex.lock(); // <-- This is not protection the GetUpdateTime method from invoking over and over.

        bWorking = true;

        int size = groupsMarkedForUpdate.size();
        if (MyApp::getInstance()->GetUpdateTime(batchVectorResult))            
        {
            bWorking = false;
            emit UpdateNotifications();                        
        }            
        m_mutex.unlock();
    }
}


void NotificationThread::run()
{
    m_NotificationTimer = new QTimer();
    connect(m_NotificationTimer, 
            SIGNAL(timeout()),
            this,
            SLOT(fire(),
            Qt::DirectConnection));

    int interval = val.toInt();
    m_NotificationTimer->setInterval(3000);
    m_NotificationTimer->start();

    QThread::exec();
}


// This method is invoked from the main class
void NotificationThread::Execute(const QStringList batchReqList)
{
    m_batchReqList = batchReqList;
    start();
}


You could always have a thread that needs to run the method connected to an onDone signal that alerts all subscribers that it is complete. Then you should not run into the problems associated with double lock check and memory reordering. Maintain the run state in each thread.


I'm assuming you want to protect your thread from calls from another thread. Am I right? If yes, then..

This is what QMutex is for. QMutex gives you an interface to "lock" the thread until it is "unlocked", thus serializing access to the thread. You can choose to unlock the thread until it is done doing its work. But use it at your own risk. QMutex presents its own problems when used incorrectly. Refer to the documentation for more information on this.

But there are many more ways to solve your problem, like for example, @Beached suggests a simpler way to solve the problem; your instance of QThread would emit a signal if it's done. Or better yet, make a bool isDone inside your thread which would then be true if it's done, or false if it's not. If ever it's true then it's safe to call the method. But make sure you do not manipulate isDone outside the thread that owns it. I suggest you only manipulate isDone inside your QThread.

Here's the class documentation: link

LOL, I seriously misinterpreted your question. Sorry. It seems you've already done my second suggestion with bWorking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜