开发者

Implement a multithreading environment

开发者 https://www.devze.com 2023-01-01 16:40 出处:网络
I want to implement a multithreading environment using Qt4. The idea is as follows in c++-alike pseudo-code:

I want to implement a multithreading environment using Qt4. The idea is as follows in c++-alike pseudo-code:

class Thread : public QThread {
    QList<SubThread*> threads_;

public:
    void run() {
        foreach(SubThread* thread : threads) {
            thread.start();
        }

        foreach(SubThread* thread : threads) {
            thread.wait();
        }
    }

    void abort() {
        foreach(SubThread* thread : threads) {
            thread.cancel();
        }
    }

public slots:
    // This method is called from the main-thread
    // (sometimes via some signal-slot-connection)
    void changeSomeSettings() {
        abort();
        // change settings
        start();
    }
}

class SubThread : public QThread {
    bool isCancelled_;

public:
    void run() {
        while(!isCancelled or task completed) {
            // something that takes some time...
        }
    }

    void cancel() {
        if(isRunning() {
  开发者_如何转开发          isCancelled_ = true;
        }
    }
}

The purpose is that the slot changeSomeSettings() kills all running threads, commits its changes and restarts it. What I want to achieve is that once this method has been started, it calls "abort" and then waits until all threads have terminated. Using mutexes in a wrong way:

    void Thread::changeSomeSettings() {
        mutex1.lock();
        abort();

        mutex2.lock();

        start();
        mutex1.unlock();
    }

    void Thread::run() {
        foreach(Thread* thread : threads) {
            thread.start();
        }

        foreach(Thread* thread : threads) {
            thread.wait();
        }

        mutex2.unlock();
    }

This actually works in Qt under MacOSX, yet according to the documentation mutex2 must be unlocked in the same thread (and in Windows I get an error). What is the best way to achieve my goal without running into racing conditions and deadlocks? Is there a better design than the one I have proposed here?


You probably want to use a condition variable instead of a mutex for this situation. A condition variable is a way for one thread to signal another. QT's implementation appears to be the QTWaitCondition:

I might have the child thread's periodically check the state of the condition variable. This can be done with QTWaitCondition::wait() with a short/0 timeout. If it is being signaled, then lock a shared memory area containing updated data and access the data that needs to be updated. Then that thread can safely restart itself accordingly.

It's usually not a good idea to just abort a thread. You may end up leaking memory/resources/handles/locks/etc. You don't know where that thread is in it's call stack, and there may be no guarantees that the stack will be "unwound" for you and all destructors are called. This is another reason for the child threads checking a condition variable periodically for updated data and having them restart themselves safely with the new data.

0

精彩评论

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

关注公众号