withLock

fun <T> Semaphore.withLock(block: () -> T): T

Acquires a permit from this Semaphore, executes block, and releases the permit in a finally block.

Return

the result of block.

Parameters

block

the code to execute while holding the permit.

Type Parameters

T

the return type of block.

Throws

if the thread is interrupted while waiting for a permit, in which case block does not run and no permit is taken or released.