The core concept is the encapsulation of concurrent threads of execution (here encompassing kernel and userland threads and processes) by way of control flow constructs that have clear entry and exit points and that ensure all spawned threads have completed before exit. Such encapsulation allows errors in concurrent threads to be propagated to the control structure's parent scope and managed by the native error handling mechanisms of each particular computer language. It allows control flow to remain readily evident by the structure of the source code despite the presence of concurrency. To be effective, this model must be applied consistently throughout all levels of the program – otherwise concurrent threads may leak out, become orphaned, or fail to have runtime errors correctly propagated.
Structured concurrency is analogous to structured programming, which introduced control flow constructs that encapsulated sequential statements and subroutines.
The fork–join model from the 1960s, embodied by multiprocessing tools like OpenMP, is an early example of a system ensuring all threads have completed before exit. However, Smith argues that this model is not true structured concurrency as the programming language is unaware of the joining behavior, and is thus unable to enforce safety.
The concept was formulated in 2016 by Martin Sústrik (creator of ZeroMQ) with his C library libdill, with goroutines as a starting point. It was further refined in 2018 by Nathaniel J. Smith, who introduced a "nursery pattern" in his Python implementation called Trio. Meanwhile, Roman Elizarov independently came upon the same ideas while developing an experimental coroutine library for the Kotlin language.
A major point of variation is how an error in one member of a concurrent thread tree is handled. Simple implementations will merely wait until the children and siblings of the failing thread run to completion before propagating the error to the parent scope. However, that could take an indefinite amount of time. The alternative is to employ a general cancellation mechanism (typically a cooperative scheme allowing program invariants to be honored) to terminate the children and sibling threads in an expedient manner.
- Smith, Nathaniel J. (25 April 2018). "Notes on structured concurrency, or: Go statement considered harmful". Retrieved 1 August 2019. CS1 maint: discouraged parameter (link)
- Sústrik, Martin (7 February 2016). "Structured Concurrency". Retrieved 1 August 2019. CS1 maint: discouraged parameter (link)
- Elizarov, Roman (12 September 2018). "Structured concurrency". Retrieved 21 September 2019. CS1 maint: discouraged parameter (link)
- Elizarov, Roman (July 2019). Structured concurrency (Videotape). Hydra Distributed computing conference. 42 minutes in. Retrieved 21 September 2019.
We needed a name and we needed to finalize this whole concept [...] and we stumble onto this blog post [...] by Nathaniel J. Smith.
- Bateman, Alan. "Structured concurrency". openjdk.java.net. OpenJDK. Retrieved November 23, 2019.
- Cohen, Ben. "Swift Concurrency Roadmap". swift.org. Swift. Retrieved October 31, 2020.