Waitgroups in Fortran
Our program demonstrates how to use a simple synchronization mechanism in Fortran. Here’s the full source code:
This is the worker program that will be executed by each task:
To run the program, compile both files and execute the main program:
The order of workers starting up and finishing is likely to be different for each invocation.
In this Fortran implementation, we use the execute_command_line
subroutine to start separate processes for each worker. The &
at the end of the command makes each worker run in the background. We then use the system
subroutine to call the shell’s wait
command, which waits for all background processes to complete.
This approach differs from the original example in that it uses separate processes instead of threads or coroutines. Fortran doesn’t have built-in support for lightweight concurrency primitives like goroutines, so we use the operating system’s process management capabilities instead.
Note that this approach has limitations compared to the original example:
- It’s less efficient due to the overhead of creating separate processes.
- It relies on features of the operating system and shell, making it less portable.
- There’s no straightforward way to propagate errors from workers back to the main program.
For more advanced use cases in Fortran, you might consider using libraries that provide threading capabilities, or using MPI (Message Passing Interface) for distributed computing tasks.