Comments (2)
Hi,
we already support something like this, although it's not so succinct as WinRt and it doesn't use thread pool - there's QCoro::moveToThread()
coroutine that allows you to change the current thread context.
QCoro::Task<> DoWorkAsync(QLabel *label)
{
// capture current thread
auto *uiThread = QThread::currentThread();
// prepare worker thread
QThread workerThread;
workerThread.start();
// we are still on UI thread, so we can interact with the UI
label->setText("Working...");
// switch current thread context to the worker thread
co_await QCoro::movetoThread(&workerThread);
// ...
// do your work here running on the workerThread
// ...
// switch thread context back to the ui thread
co_await QCoro::moveToThread(uiThread);
// back on the main thread, we can interact with UI agin.
label->setText("Done");
// stop the worker thread, we don't need it anymore
workerThread.quit();
workerThread.wait();
}
Managing the worker thread could be hidden into a RAII class, something like the code below, although I'm not sure whether that's something that belongs to QCoro.
class WorkerThread {
public:
explicit WorkerThread()
: mThread(new QThread())
{
mThread->start();
}
QThread *thread() const { return mThread; }
~WorkerThread()
{
// NOTE: this needs a much more careful approach to avoid leaks, this code is just for example
if (mThread->isRunning()) {
connect(mThread, &QThread::finished, mThread, &QObject::deleteLater);
mThread->quit();
} else {
mThread->deleteLater();
}
}
private:
QThread *mThread = nullptr;
};
That makes the code much nicer:
QCoro::Task<> DoWorkAsync(QLabel *label)
{
auto *uiThread = QThread::currentThread();
WorkerThread workerThread;
label->setText("Working...");
co_await QCoro::movetoThread(workerThread.thread());
// do your work here running on the workerThread
co_await QCoro::moveToThread(uiThread);
label->setText("Done");
}
It should even be possible to craft integration with QThreadPool so that one does not have to create the worker thread manually and get some throttling on the number of background tasks. But same as with WorkerThread
, I'm not sure whether that's something that belongs to QCoro - the scope of QCoro originally was to make it possible to interact with Qt types using C++ coroutines.
But there's also been requests for SQL integration (#188), I've been thinking about native async file IO classes and here you are with those threading primitives, so I'll have to re-consider the scope or create a QCoroExtras library....
from qcoro.
This looks pretty good already
from qcoro.
Related Issues (20)
- `CheckAtomic.cmake` Fails on Windows with LLVM Clang
- Build Failure with Clang `15.x`< on Windows in Debug
- QCoroSignal crashes
- QCoro::waitFor does not rethrow exceptions
- Add latest Qt6 to CI
- Bind coroutine to a QObject lifetime HOT 6
- Make not co_awaiting a coroutine trigger -Wunused-result HOT 1
- Add this library to vcpkg HOT 1
- Apple Clang ICEs in Release build of QCoro HOT 1
- Update QCoro in ConanCenter
- Add support to work with database HOT 2
- Next release HOT 3
- ASSERT: "this->mAwaitedCoroutine != nullptr" when awaiting a default-constructed Task HOT 4
- support mqtt HOT 2
- Qt Design Studio : Mocking QCoro::QmlTask in Javascript or QML HOT 3
- Clang 14.x ornery about std::experimental::coroutine_traits HOT 3
- Illogical result of the `write` function.
- Bug: application crashes when signal from another thread timeouts HOT 1
- Support for move only types in QFuture via takeResult
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qcoro.