3

Intro

Structured concurrency is a relatively recent concept for structuring concurrent programs. It has implementations in for example Python, Java and Swift.

Examples of structured concurrency often describe a server application which has to receive or make multiple requests on the same time.

Like this:

async with trio.open_nursery() as nursery:
    nursery.start_soon(myfunc)
    nursery.start_soon(anotherfunc)

GUI applications

GUI applications on the other hand often have a different pattern of concurrency: There is a GUI thread which must never block for long. The GUI thread might spawn background jobs which run concurrently and reports back to the GUI thread.

The GUI thread can not create a "nursery" and wait for all its associated child-tasks to complete. It has the be available to perform GUI tasks.

Question

Can the concept of structured concurrency be applied to an application which follows this pattern? How would that look?

1 Answer 1

1

There is no conflict. A typical GUI has a message loop that picks up messages from a queue until the queue is empty. Any worker thread can post results to that queue which will then be picked up and handled by the GUI thread.

4
  • But how does structured concurrency make a difference in this case? How would it be applied?
    – Lii
    Jun 5, 2022 at 8:32
  • @Lii It doesn't, there is only one GUI thread. You can do all the structured concurrency you want in your program, the GUI thread will not be part of it. The point of structured concurrency as I understand it is to have a language supported way of starting threads and awaiting the results, like the async-await construct in C#. The GUI thread never ends. You don't start it and it doesn't yield any results, so the GUI thread will always be independent of any applied structured concurrency. Jun 5, 2022 at 12:05
  • This is my impression also, that structured concurrency can not be applied on background jobs in a GUI application. But in the descriptions that I've seen they sound like you should be able to replace all existing concurrency patterns with structured concurrency. Because of this I though that maybe I'm missing something.
    – Lii
    Jun 6, 2022 at 18:35
  • @Lii Although the GUI thread itself will not participate in the structured concurrency model it can still use it to manage worker threads. It does not have to block while the worker threads are busy, that would defeat the purpose of the multi-threading exercise. It can have an event handler called when a worker thread finishes or periodically (timer based) check for results. Jun 6, 2022 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.