Mutexes in Visual Basic .NET
In the previous example we saw how to manage simple counter state using atomic operations. For more complex state we can use a mutex to safely access data across multiple threads.
The Container
class holds a dictionary of counters. Since we want to update it concurrently from multiple threads, we add a lock object to synchronize access. Note that in VB.NET, we use SyncLock
instead of a Mutex
for simplicity in this example.
The Increment
method locks the object before accessing _counters
, ensuring thread-safe access.
In the Main
method, we create an instance of Container
and define a lambda function doIncrement
that increments a named counter in a loop.
We then create multiple tasks to run doIncrement
concurrently, simulating concurrent access to the same Container
instance.
After waiting for all tasks to complete, we print the final state of the counters.
Running the program shows that the counters are updated as expected:
In this VB.NET version, we’ve used Task
s and the Task.Run
method to create concurrent operations, which is analogous to goroutines in Go. The SyncLock
statement provides mutual exclusion, similar to the Mutex
in the original Go code.
Next, we could look at implementing this same state management task using only asynchronous methods and message passing, which would be more similar to the concept of goroutines and channels in Go.