Skip to content

How to properly handle shared hardware resources? #103

Answered by danvratil
DeveloperPaul123 asked this question in Q&A
Discussion options

You must be logged in to vote

I think the coroutines are a good solution for this - consider that whenever a QCoro coroutine is suspended, the execution returns to caller (so, usually the Qt event loop). Thus you can have as many "service" coroutines as you want and always only one is executing at a time, the others are suspended (as long as everything is single-threaded, of course) - same as Qt slots.

If I understand your description correctly, a fictional Comm class might look something like this:

// Heartbeat coroutine
QCoro::Task<> Comm::heartbeat() {
    QTimer sleep;
    sleep.start(1s);
    while (mSocket->open()) {
        co_await sleep;
        const auto stats = co_await command("default");
    }
}

// Send…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@danvratil
Comment options

@DeveloperPaul123
Comment options

@DeveloperPaul123
Comment options

@danvratil
Comment options

@DeveloperPaul123
Comment options

Answer selected by DeveloperPaul123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants