Product Site

5.4.12.2. acquire

Returns .true if the current activity has already owned or has just acquired the mutex semaphore. Returns .false if the mutex is owned by a different activity, or a timeout has occurred.

Nested acquires, from an activity already owning the mutex semaphore, are allowed, with each acquire increasing the mutex nesting level by one. An equivalent number of calls to release are needed to make the mutex available again to another activity.

If timeout is specified it must be a TimeSpan instance or a valid Rexx number. If the value is negative or if timeout is omitted, acquire suspends the current activity until it can get ownership of the mutex.

If timeout is zero, acquire immediately returns .true if the mutex was acquired, or .false otherwise.

If the timeout period is positive, acquire suspends the current activity for timeout seconds or until the current activity can acquire the mutex, whatever comes first.

If an activity still owns mutex semaphores when it ends, these semaphores will be automatically released by the interpreter.

See also method release.

Example 5.272. MutexSemaphore class — acquire method
mutex = .MutexSemaphore~new
.Task~new~startWork(mutex, "work 1")
.Task~new~startWork(mutex, "work 2")
say "work tasks started"

::class Task

::method startWork unguarded
  expose mutex name
  use strict arg mutex, name
  reply
  self~doWork(1)

::method doWork unguarded
  expose mutex name
  use strict arg level
  -- five levels of nested acquires
  if level > 5 then
    return
  mutex~acquire
  say name level
  self~doWork(level + 1)

may output
work tasks started
work 2 1
work 2 2
work 2 3
work 2 4
work 2 5
work 1 1
work 1 2
work 1 3
work 1 4
work 1 5