Structured concurrency: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
m Dating maintenance tags: {{Citation needed}}
Adding local short description: "Programming paradigm for improving clarity and development time of a computer program", overriding Wikidata description "programming paradigm aimed at improving programming via a structured approach to concurrent programming"
 
(28 intermediate revisions by 11 users not shown)
Line 1:
{{Short description|Programming paradigm for improving clarity and development time of a computer program}}
{{Programming paradigms}}
'''Structured concurrency''' is a [[programming paradigm]] aimed at improving the clarity, quality, and development time of a [[computer program]] by using a structured approach to [[concurrent computing|concurrent programming]].
 
'''StructuredThe concurrency'''core concept is athe [[programmingencapsulation paradigm]]of aimedconcurrent atthreads improvingof theexecution clarity,(here quality,encompassing kernel and developmentuserland timethreads and processes) by way of acontrol [[computerflow program]]constructs bythat usinghave aclear structuredentry approachand toexit [[concurrentpoints computing|concurrentand programming]].that Theensure coreall conceptspawned isthreads thathave whencompleted controlbefore splitsexit. intoSuch concurrentencapsulation tasksallows thaterrors theyin joinconcurrent upthreads again,to andbe thatpropagated thisto the control flowstructure's isparent readilyscope evidentand managed by the structurenative oferror thehandling sourcemechanisms code.of Ifeach aparticular "maincomputer task"language. splits intoIt severalallows concurrentcontrol sub-tasks scheduledflow to beremain executedreadily inevident fibersby thenthe thosestructure fibersof mustthe terminatesource beforecode despite the mainpresence taskof can completeconcurrency. To be effective, this model must be applied consistently throughout all levels of the program-- – otherwise concurrent tasksthreads may leak out, become orphaned, or fail to have runtime errors correctly propagated.
 
Structured concurrency is analogous to [[structured programming]], which uses control flow constructs that encapsulate sequential statements and subroutines.
The naming is inspired by [[structured programming]], which created higher level control-flow statements out of the very basic [[goto]]. Similarly, structured concurrency creates higher level mechanisms for concurrency constructs out of the basic concurrent task creation constructs ([[Spawn (computing)|spawn]], [[Thread (computing)|thread]]s, [[Fiber (computer science)|fiber]]s).
 
== History ==
The concept was formulated and named "structured concurrency" in 2016 by Martin Sústrik (creator of [[ZeroMQ]])<ref>{{cite web |last1=Sústrik |first1=Martin |title=Structured Concurrency |url=http://250bpm.com/blog:71 |date=7 February 2016 |accessdate=1 August 2019}}</ref>. Without knowledge of Sústrik's work{{Citation needed|date=September 2019}}, the concept was independently offered in April 2018 by Nathaniel J. Smith, who implemented it in the [https://trio.readthedocs.io/en/stable/ Trio] library for Python<ref>{{cite web |last1=Smith |first1=Nathaniel J. |title=Notes on structured concurrency, or: Go statement considered harmful |url=https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ |date=25 April 2018 |accessdate=1 August 2019}}</ref>, and in September 2018 by Roman Elizarov, while developing an experimental coroutine library for the Kotlin language<ref>{{cite web |last1=Elizarov |first1=Roman |title=Structured concurrency |url=https://medium.com/@elizarov/structured-concurrency-722d765aa952 |date=12 September 2018 |accessdate=21 September 2019}}</ref>. (Smith happened to give the same name to concept, while Elizarov adopted the name after reading Smith's article, shortly before publishing his own work for Kotlin<ref>{{cite AV media |people=Elizarov, Roman |date=July 2019 |title=Structured concurrency |medium=Videotape |language=en |url=https://youtube.com/watch?v=Mj5P47F6nJg&t=2538 |access-date=21 September 2019 |publisher=Hydra Distributed computing conference |minutes=42 minutes in |quote="[...] we needed a name and we needed to finalize this whole concept [...] and [Nathaniel J. Smith] got a really good name for it, he called it structured concurrency."}}</ref>.)
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.<ref>{{cite web |last1=Smith |first1=Nathaniel J. |title=Notes on structured concurrency, or: Go statement considered harmful |url=https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ |date=25 April 2018 |access-date=1 August 2019}}</ref>
 
The concept was formulated and named "structured concurrency" in 2016 by Martin Sústrik (creatora developer of [[ZeroMQ]]) with his C library libdill, with [[Go_(programming_language)#Concurrency:_goroutines_and_channels|goroutines]] as a starting point.<ref>{{cite web |last1=Sústrik |first1=Martin |title=Structured Concurrency |url=http://250bpm.com/blog:71 |date=7 February 2016 |accessdateaccess-date=1 August 2019}}</ref>. Without knowledge of Sústrik's work{{Citation needed|date=September 2019}}, the conceptIt was independentlyfurther offeredrefined in April 20182017 by Nathaniel J. Smith, who implementedintroduced ita "nursery pattern" in thehis [https://trio.readthedocs.io/en/stable/[Python (programming Triolanguage)|Python]] libraryimplementation forcalled PythonTrio.<ref>{{cite web |last1=Smith |first1=Nathaniel J. |title=NotesAnnouncing on structured concurrency, or: Go statement considered harmfulTrio |url=https://vorpus.org/blog/notesannouncing-on-structured-concurrency-or-go-statement-considered-harmfultrio/ |date=2510 AprilMarch 20182017 |accessdateaccess-date=123 AugustSeptember 20192022}}</ref> Meanwhile, andRoman inElizarov Septemberindependently 2018came byupon Romanthe Elizarov,same ideas while developing an experimental [[coroutine]] library for the [[Kotlin (programming language)|Kotlin language]],<ref>{{cite web |last1=Elizarov |first1=Roman |title=Structured concurrency |url=https://medium.com/@elizarov/structured-concurrency-722d765aa952 |date=12 September 2018 |accessdateaccess-date=21 September 2019}}</ref>. (Smith happened to give the same name to concept, while Elizarov adopted the name after reading Smith's article, shortly before publishing his own work for Kotlin<ref>{{cite AV media |people=Elizarov, Roman |date=11 July 2019 |title=Structured concurrency |medium=VideotapeVideo |language=en |url=https://youtube.com/watch?v=Mj5P47F6nJg&t=2538 |access-date=21 September 2019 |publisher=Hydra Distributed computing conference |minutes=42 minutes in |quote="[...] weWe needed a name and we needed to finalize this whole concept [...] and we stumble onto this blog post [...] by Nathaniel J. Smith]."}}</ref> which later gotbecame a reallystandard goodlibrary.<ref>{{cite nameweb for| it,url=https://kotlinlang.org/docs/coroutines-basics.html#structured-concurrency he| calledtitle=Coroutines itbasics: structuredStructured concurrency." | website=Kotlin | publisher=JetBrains | access-date=3 March 2022}}</ref>.)
==See also==
 
In 2021, [[Swift (programming language)|Swift]] adopted structured concurrency.<ref>{{cite web |first1=John |last1=McCall |first2=Joe |last2=Groff |first3=Doug |last3=Gregor |first4=Konrad |last4=Malawski |access-date=3 March 2022 |title=Swift Structured Concurrency Proposal |website=Apple's Swift Evolution repo |publisher=GitHub |url=https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md}}</ref> Later that year, a draft proposal was published to add structured concurrency to [[Java (programming language)|Java]].<ref>{{cite web |last1=Pressler |first1=Ron |website=[[OpenJDK]] |publisher=Oracle |access-date=3 March 2022 |title=JEP draft: Structured Concurrency (Incubator) |url=https://openjdk.java.net/jeps/8277129}}</ref>
 
== Variations ==
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.
 
== See also ==
* [[Structured programming]]
 
== References ==
=== Citations ===
{{Reflist}}
 
=== External links===
* [https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ Notes on structured concurrency, or: Go statement considered harmful] by Nathaniel J. Smith
* [https://wiki.openjdk.java.net/display/loom/Structured+Concurrency Structured Concurrency], Alan Bateman, [[OpenJDK]] wiki
* [https://trio.discourse.group/c/structured-concurrency Structured concurrency forum], cross-computer-language discussion of structured concurrency with participation by Sústrik, Smith, and Elizarov
* [https://archive.fosdem.org/2019/schedule/event/structured_concurrency/ FOSDEM 2019: Structured Concurrency], lightning talk by Martin Sustrik with links to some implementations
 
{{Programming paradigms navbox}}
 
[[Category:Programming paradigms]]