c++ - BlockingQueue 的 QWaitCondition : Destroyed while threads are still waiting

标签 c++ qt exception qthread

我在 Qt 中构建了自己的阻塞队列,但遇到了一些问题。如果我不关闭队列,那么我会在控制台中收到错误消息“QWaitCondition:线程仍在等待时已销毁”。另一方面,我在关闭队列后收到访问冲突异常(无论它是在构造函数中还是来自另一个线程)。异常发生在等待条件的 wait 方法中。

这是我的阻塞队列:

#ifndef BLOCKING_QUEUE_H
#define BLOCKING_QUEUE_H

#include <QObject>
#include <QSharedPointer>
#include <QWaitCondition>
#include <QMutex>
#include <queue>

namespace Concurrency
{
    template<typename Data>
    class BlockingQueue
    {
    private:
        QMutex _mutex;
        QWaitCondition _monitor;
        volatile bool _closed;
        std::queue<QSharedPointer<Data>> _queue;

    public:
        BlockingQueue()
        {
            _closed = false;
        }

        ~BlockingQueue()
        {
            Close(); // When this is enabled, I get an access violation exception in TryDequeue
        }

        void Close()
        {
            QMutexLocker locker(&_mutex);
            if(!_closed)
            {
                _closed = true;
                _queue.empty();
                _monitor.wakeAll();
            }
        }

        bool Enqueue(QSharedPointer<Data> data)
        {
            QMutexLocker locker(&_mutex);

            // Make sure that the queue is not closed
            if(_closed)
            {
                return false;
            }

            _queue.push(data);

            // Signal all the waiting threads
            if(_queue.size()==1)
            {
                _monitor.wakeAll();
            }

            return true;
        }

        bool TryDequeue(QSharedPointer<Data>& value, unsigned long time = ULONG_MAX)
        {
            QMutexLocker locker(&_mutex);

            // Block until something goes into the queue
            // or until the queue is closed
            while(_queue.empty())
            {
                if(_closed || !_monitor.wait(&_mutex, time)) // <-- Access violation if I call close in the destructor
                {
                    return false;
                }
            }

            // Dequeue the next item from the queue
            value = _queue.front();
            _queue.pop();
            return true;
        }
    };
}
#endif BLOCKING_QUEUE_H

我假设发生这种情况是因为等待的线程在队列已经被销毁并且互斥锁随后也被销毁后收到信号。当线程在 TryDequeue 中被唤醒时,不再分配互斥量,因此会导致访问冲突异常。避免这种情况的最佳方法是什么?

最佳答案

我遇到的问题与线程方法有关,有关更多信息,请查看以下问题:Why is QThread::finished signal not being emitted?

使用 BlockingQueue 的服务没有正确关闭在队列中等待的线程,随后导致队列被销毁,而线程仍在 中等待TryDequeue 方法。

关于c++ - BlockingQueue 的 QWaitCondition : Destroyed while threads are still waiting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424586/

相关文章:

c++ - 奇怪的错误:在没有真正创建指针时使用已删除的函数 'std::unique_ptr<_Tp, _Dp>::unique_ptr

C++:将 try/catch 添加到代码时错误消失

cocoa - 从 cocoa 应用程序中使用 Qt 小部件

javascript - 向现有 QT 应用程序添加 javascript 支持

c++ - 是否有可能从对象的实例中获取指向成员的指针?

c++ - 正确获取 sizeof()

c++ - 在给定指向父类(super class)的指针的子类上调用方法

Java 程序不会抛出所需的异常

.net - 使用 Windows Azure 托管的连接问题

php - 为什么在php中报告异常时使用new