Mutexes in Co-array Fortran
In the previous example, we saw how to manage simple counter state using atomic operations. For more complex state, we can use a critical section to safely access data across multiple images.
In this Co-array Fortran version:
We define a
container
type that holds an array of counters as a co-array.The
inc
subroutine increments a named counter within a critical section to ensure thread safety.In the main program, we initialize the counters to zero.
We then perform increments across multiple images:
- All images increment the first counter 10000 times.
- Image 1 additionally increments the first counter another 10000 times and the second counter 10000 times.
We use
sync all
to ensure all images have completed their operations before printing the results.Finally, we print the counters from image 1.
To run the program:
This example demonstrates how to use critical sections in Co-array Fortran to safely manage shared state across multiple images, which is analogous to using mutexes in other languages.