initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,55 @@
|
||||
#include "Cevo/Threading.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QEventLoop>
|
||||
#include <QThread>
|
||||
|
||||
#include <exception>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
void runInThread(const std::function<void()> &method)
|
||||
{
|
||||
std::exception_ptr failure;
|
||||
QThread *thread = QThread::create([&method, &failure]() {
|
||||
try {
|
||||
method();
|
||||
} catch (...) {
|
||||
failure = std::current_exception();
|
||||
}
|
||||
});
|
||||
|
||||
if (QThread::currentThread() == qApp->thread()) {
|
||||
QEventLoop loop;
|
||||
QObject::connect(thread, &QThread::finished, &loop, &QEventLoop::quit);
|
||||
thread->start();
|
||||
loop.exec();
|
||||
} else {
|
||||
thread->start();
|
||||
thread->wait();
|
||||
}
|
||||
delete thread;
|
||||
|
||||
if (failure)
|
||||
std::rethrow_exception(failure);
|
||||
}
|
||||
|
||||
void runInThreadAsync(const std::function<void()> &method, const std::function<void()> &callback)
|
||||
{
|
||||
QThread *thread = QThread::create(method);
|
||||
if (callback)
|
||||
QObject::connect(thread, &QThread::finished, thread, callback);
|
||||
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
||||
thread->start();
|
||||
}
|
||||
|
||||
void synchronizeToMainThread(const std::function<void()> &method)
|
||||
{
|
||||
if (QThread::currentThread() == qApp->thread()) {
|
||||
method();
|
||||
return;
|
||||
}
|
||||
QMetaObject::invokeMethod(qApp, method, Qt::BlockingQueuedConnection);
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user