refs #336 Fixed CLogHandler thread safety in swiftcore.

This commit is contained in:
Mathew Sutcliffe
2014-10-16 16:36:53 +01:00
parent d0894d2cf9
commit c30bf9f61e
4 changed files with 33 additions and 13 deletions

View File

@@ -14,12 +14,33 @@
#include <QThread>
#include <QMutex>
#include <QTimer>
#include <functional>
#include <atomic>
namespace BlackMisc
{
/*!
* Starts a single-shot timer which will run in an existing thread and call a task when it times out.
*
* Useful when a worker thread wants to push small sub-tasks back to the thread which spawned it.
* \see QTimer::singleShot()
*/
template <typename F>
void singleShot(int msec, QThread *target, F task)
{
auto *timer = new QTimer;
timer->setSingleShot(true);
timer->moveToThread(target);
QObject::connect(timer, &QTimer::timeout, [ = ]()
{
task();
timer->deleteLater();
});
QMetaObject::invokeMethod(timer, "start", Q_ARG(int, msec));
}
/*!
* Just a subclass of QThread whose destructor waits for the thread to finish.
*/