Table of Contents

Class Mutex

Namespace
Godot
Assembly
GodotSharp.dll

A synchronization mutex (mutual exclusion). This is used to synchronize multiple GodotThreads, and is equivalent to a binary Semaphore. It guarantees that only one thread can access a critical section at a time.

This is a reentrant mutex, meaning that it can be locked multiple times by one thread, provided it also unlocks it as many times.

Warning: Mutexes must be used carefully to avoid deadlocks.

Warning: To ensure proper cleanup without crashes or deadlocks, the following conditions must be met:

- When a Mutex's reference count reaches zero and it is therefore destroyed, no threads (including the one on which the destruction will happen) must have it locked.

- When a GodotThread's reference count reaches zero and it is therefore destroyed, it must not have any mutex locked.

public class Mutex : RefCounted, IDisposable
Inheritance
Mutex
Implements
Inherited Members

Constructors

Mutex()

public Mutex()

Methods

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

Lock()

Locks this Mutex, blocks until it is unlocked by the current owner.

Note: This function returns without blocking if the thread already has ownership of the mutex.

public void Lock()

TryLock()

Tries locking this Mutex, but does not block. Returns true on success, false otherwise.

Note: This function returns true if the thread already has ownership of the mutex.

public bool TryLock()

Returns

bool

Unlock()

Unlocks this Mutex, leaving it to other threads.

Note: If a thread called Lock() or TryLock() multiple times while already having ownership of the mutex, it must also call Unlock() the same number of times in order to unlock it correctly.

Warning: Calling Unlock() more times that Lock() on a given thread, thus ending up trying to unlock a non-locked mutex, is wrong and may causes crashes or deadlocks.

public void Unlock()