Next: Atomic memory accesses and memory ordering, Previous: Task-local data, Up: Pthreads [Contents][Index]
Pthreads tasks run concurrently, so they need a mechanism to coordinate access to resources (while that is not necessary for cooperative multitasking).
Mutexes can only be locked by one thread at one time, all other threads have to wait until the mutex is unlocked.
mutex ( "name" – ) gforth-experimental
create a named mutex name, initially unlocked.
name execution: ( – mutex )
lock ( mutex – ) gforth-experimental
If mutex is currently locked, wait until it is unlocked. Lock mutex.
unlock ( mutex – ) gforth-experimental
Unlock mutex.
You can ensure that you always unlock a mutex after locking it by using:
critical-section ( xt mutex – ) gforth-experimental
If mutex is currently locked, wait until it is unlocked. Lock mutex, then execute xt. After xt finishes (regularly, or through an exception), unlock mutex.