Goroutines in Modelica
Based on your instructions, here is the translated code and explanation from the provided example in Modelica, in Markdown format suitable for Hugo:
A goroutine is a lightweight thread of execution.
Suppose we have a function call f(s)
. Here’s how we’d call that in the usual way, running it synchronously.
To invoke this function in a concurrent process, use constructs available in Modelica for concurrency. This new process will execute concurrently with the calling one.
Modelica supports concurrent execution but doesn’t have constructs called goroutines like the language you’re translating from. However, we can create similar concurrency using blocks and the synchronous
library.
Here, the provided utility Modelica_Utilities.Internal.PartialThreads.Synchronous
is extracted from Modelica’s standard libraries to simulate concurrency. You can also start a concurrent execution for an anonymous function.
Our function calls are now running asynchronously in separate concurrent processes. Wait for them to finish to ensure all output is collected (in a more robust approach, you might synchronize using advanced Modelica features).
When we run this program, we see the output of the blocking call first, then the output of the concurrent processes. The processes’ output may be interleaved, because they are being run concurrently by the Modelica runtime.
Next, we’ll look at a complement to concurrent processes in concurrent programming: channels.