Interfaces

interface Condition

public interface Condition {
    func notify(): Unit
    func notifyAll(): Unit
    func wait(): Unit
    func wait(timeout!: Duration): Bool
    func waitUntil(predicate: ()->Bool): Unit
    func waitUntil(predicate: ()->Bool, timeout!: Duration): Bool
}

Description: Provides an interface that can block a thread and wait for a signal from another thread to resume execution.

This is a mechanism that uses the shared variable for thread synchronization. When some threads are suspended due to waiting for a condition of the shared variable to be met, other threads change the shared variable so that the condition is met. Then, a wakeup operation is performed. As a result, the execution of the suspended thread can continue after the thread is woken up.

func notify()

func notify(): Unit

Description: Wakes up a thread waiting on the associated mutex.

Throws:

func notifyAll()

func notifyAll(): Unit

Description: Wakes up all threads waiting on the associated mutex.

Throws:

func wait()

func wait(): Unit

Description: Suspends the current thread until the corresponding notify function is called.

NOTE

When a thread enters the waiting state, the corresponding mutex is released. After being woken up, the thread holds the mutex again.

Throws:

func wait(Duration)

func wait(timeout!: Duration): Bool

Description: Suspends the current thread until the corresponding notify function is called or the suspension time exceeds timeout.

NOTE

When a thread enters the waiting state, the corresponding mutex is released. After being woken up, the thread holds the mutex again.

Parameters:

Returns:

Throws:

func waitUntil(()->Bool)

func waitUntil(predicate: ()->Bool): Unit

Description: Suspends the current thread until the corresponding notify function is called and the predicate result is true.

NOTE

  • When a thread enters the waiting state, the corresponding mutex is released. After being woken up, the thread holds the mutex again.
  • This method checks whether the predicate result is true. If so, the result is returned. If not, the current thread is suspended.

Parameters:

  • predicate: ()->Bool: Wait until the result is true.

Throws:

func waitUntil(()->Bool,Duration)

func waitUntil(predicate: ()->Bool, timeout!: Duration): Bool

Description: Suspends the current thread until the corresponding notify function is called and the predicate result is true, or the suspension time exceeds timeout.

NOTE

  • When a thread enters the waiting state, the corresponding mutex is released. After being woken up, the thread holds the mutex again.
  • This method checks whether the predicate result is true. If so, true is returned. If not, the current thread is suspended.

Parameters:

  • predicate: ()->Bool: Wait until the result is true.
  • timeout!: Duration: suspension time. The default value is Duration.Max.

Returns:

  • Bool: If Monitor (deprecated) is woken up by another thread and the predicate result is true, true is returned. If timeout occurs, false is returned.

Throws:

interface IReentrantMutex (deprecated)

public interface IReentrantMutex {
    func lock(): Unit
    func tryLock(): Bool
    func unlock(): Unit
}

Description: Provides an interface for implementing reentrant mutexes.

NOTE

  • This interface will be deprecated in the future releases and Lock will be used instead.
  • When implementing this interface, ensure that the underlying mutex supports nested locks. Otherwise, a deadlock may occur during nested use.

func lock()

func lock(): Unit

Description: Locks the mutex.

If the mutex is locked, the current thread is blocked.

func tryLock()

func tryLock(): Bool

Description: Attempts to lock the mutex.

Returns:

  • Bool: If the mutex is locked, false is returned. Otherwise, lock the mutex and true is returned.

func unlock()

func unlock(): Unit

Description: Unlocks the mutex.

If the mutex is locked for N times, you need to call this function for N times to unlock the mutex. Once the mutex is completely unlocked, if other threads are blocked on the mutex, one of the threads is woken up.

Throws:

interface Lock

public interface Lock {
    func lock(): Unit
    func tryLock(): Bool
    func unlock(): Unit
}

Description: Provides an interface for implementing reentrant mutexes.

NOTE

When implementing this interface, ensure that the underlying mutex supports nested locks. Otherwise, a deadlock may occur during nested use.

func lock()

func lock(): Unit

Description: Locks the mutex.

If the mutex is locked, the current thread is blocked.

func tryLock()

func tryLock(): Bool

Description: Attempts to lock the mutex.

Returns:

  • Bool: If the mutex is locked, false is returned. Otherwise, lock the mutex and true is returned.

func unlock()

func unlock(): Unit

Description: Unlocks the mutex.

If the mutex is locked for N times, you need to call this function for N times to unlock the mutex. Once the mutex is completely unlocked, if other threads are blocked on the mutex, one of the threads is woken up.

Throws:

interface UniqueLock

public interface UniqueLock <: Lock {
    func condition(): Condition
}

Description: Provides an interface for implementing exclusive locks.

Parent Type:

func condition()

func condition(): Condition

Description: Creates a condition related to the Lock.

It can be used to implement concurrency primitives of "single lock and multiple waiting queues."

Returns:

Throws: