Atomic Counters in GDScript
Our primary mechanism for managing state in GDScript is through signals and properties. However, there are other options for managing state. Here we’ll look at using a mutex for thread-safe counters accessed by multiple threads.
We expect to get exactly 50,000 operations. If we had used a non-thread-safe integer and incremented it without a mutex, we’d likely get a different number, changing between runs, because the threads would interfere with each other.
To run this script, attach it to a Node in your Godot scene and run the scene. You should see the following output in the console:
Note that GDScript doesn’t have built-in atomic operations like Go does. Instead, we use a Mutex to ensure thread-safe access to our shared counter. This approach is more general and can be used for more complex operations, but it may have slightly more overhead than atomic operations for simple cases like this.
Also, keep in mind that Godot’s threading model is cooperative, not preemptive. This means that threads in GDScript won’t be interrupted mid-execution by the operating system. However, using proper synchronization is still important for correctness and to ensure that changes made in one thread are visible to other threads.
Next, we’ll look at other synchronization primitives available in GDScript for managing state in multithreaded scenarios.