A mutex is basically a way to say "THIS IS MYN NO ONE ELSE CAN HAVE IT!".
If you image a mutex to be a small pebble. Everyone wants the pebble, but only one person can hold it at a time. Everyone else wanting the pebble has to wait in line. When the person holding the pebble drops it, the next person in line picks it up.
Example:
If you imagine that 10 threads are running, and they are all trying to use the function at the same time. They would all be trying to access the global variable at the same time. By locking the mutex any other attempts to lockmutex will wait in a queue until it has been unlocked.
The function LockMutex halts the thread at that line of code until the system says its your turn to hold the pebble.
If you image a mutex to be a small pebble. Everyone wants the pebble, but only one person can hold it at a time. Everyone else wanting the pebble has to wait in line. When the person holding the pebble drops it, the next person in line picks it up.
Example:
Global counter_mutex = CreateMutex() Global myvalue:Int = 10 Function increase_multiply(namount) LockMutex(mutex) myvalue = myvalue * namount UnlockMutex(mutex) End Function
If you imagine that 10 threads are running, and they are all trying to use the function at the same time. They would all be trying to access the global variable at the same time. By locking the mutex any other attempts to lockmutex will wait in a queue until it has been unlocked.
The function LockMutex halts the thread at that line of code until the system says its your turn to hold the pebble.