Code Monkey home page Code Monkey logo

Comments (2)

danvratil avatar danvratil commented on July 17, 2024

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.

stolen-programmer avatar stolen-programmer commented on July 17, 2024

This looks pretty good already

from qcoro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.