On Windows, warn if a thread was terminated with its QThread still in running state.

I hope this catches if the data cache serializer is killed in the middle of a save.
This commit is contained in:
Mathew Sutcliffe
2016-08-10 16:50:54 +01:00
parent f8fc82019d
commit a30eb18a9c
2 changed files with 47 additions and 5 deletions

View File

@@ -12,8 +12,46 @@
#include <future>
#ifdef Q_OS_WIN32
#include <windows.h>
#endif
namespace BlackMisc
{
void CRegularThread::run()
{
#ifdef Q_OS_WIN32
m_handle = GetCurrentThread();
QThread::run();
m_handle = nullptr;
#else
QThread::run();
#endif
}
CRegularThread::~CRegularThread()
{
#ifdef Q_OS_WIN32
auto handle = m_handle.load();
if (handle)
{
auto status = WaitForSingleObject(handle, 0);
if (isRunning())
{
switch (status)
{
default:
case WAIT_FAILED: qWarning() << "Thread" << objectName() << "unspecified error"; break;
case WAIT_OBJECT_0: qWarning() << "Thread" << objectName() << "unsafely terminated by program shutdown"; break;
case WAIT_TIMEOUT: break;
}
}
}
#endif
quit();
wait();
}
CWorker *CWorker::fromTaskImpl(QObject *owner, const QString &name, int typeId, std::function<CVariant()> task)
{
auto *thread = new CRegularThread(owner);

View File

@@ -32,6 +32,7 @@
#include <algorithm>
#include <functional>
#include <type_traits>
#include <atomic>
namespace BlackMisc
{
@@ -96,11 +97,14 @@ namespace BlackMisc
CRegularThread(QObject *parent = nullptr) : QThread(parent) {}
//! Destructor
~CRegularThread()
{
quit();
wait();
}
~CRegularThread();
protected:
//! \copydoc QThread::run
virtual void run() override;
private:
std::atomic<void *> m_handle { nullptr };
};
/*!