Skip to main content
Module

x/tstl/base/thread/ILockable.ts>ILockable

TypeScript-STL (Standard Template Library, migrated from C++)
Go to Latest
interface ILockable
import { type ILockable } from "https://deno.land/x/tstl@v2.4.11/base/thread/ILockable.ts";

Common interface for lockable mutex.

Methods

lock(): Promise<void>

Locks the mutex.

Monopolies a mutex until be unlock | unlocked. If there're someone who have already lock | monopolied the mutex, the function call would be blocked until all of them to return their acquistions by calling the unlock method.

In same reason, if you don't call the unlock function after your business, the others who want to lock | monopoly the mutex would be fall into the forever sleep. Therefore, never forget to calling the unlock function or utilize the UniqueLock.lock function instead to ensure the safety.

try_lock(): Promise<boolean>

Tries to lock the mutex.

Attempts to monopoly a mutex without blocking. If succeeded to monopoly the mutex immediately, it returns true directly. Otherwise there's someone who has already lock | monopolied the mutex, the function gives up the trial immediately and returns false directly.

Note that, if you succeeded to monopoly the mutex (returns true) but do not call the unlock function after your business, the others who want to lock | monopoly the mutex would be fall into the forever sleep. Therefore, never forget to calling the unlock function or utilize the UniqueLock.try_lock function instead to ensure the safety.

unlock(): Promise<void>

Unlocks the mutex.

When you call this unlock method and there're someone who are currently blocked by attempting to lock this mutex, one of them (FIFO; first-in-first-out) would acquire the lock and continues its execution.

Otherwise, there's not anyone who is acquiring the lock of this mutex, the DomainError exception would be thrown.

As you know, when you succeeded to acquire the lock, you don't have to forget to calling this unlock method after your business. If you forget it, it would be a terrible situation for the others who're attempting to lock this mutex.

However, if you utilize the UniqueLock, you don't need to consider about this unlock method. Just define your business into a callback function as a parameter of methods of the UniqueLock, then this unlock method would be automatically called by the UniqueLock after the business.