class · System.Threading
Mutex
public class Mutex
A lock: one thread at a time between Lock and Unlock (not re-entrant: a thread that holds it must not lock it again).
Run(() => …) holds it around a function. Dispose frees its handle (an undisposed one is freed when the object dies).
- Tipo
- class
- Espacio de nombres
- System.Threading
- Ensamblado
- System.Core
- Acceso
- public
- Archivo fuente
Threading.ant
Herencia
Constructores
new()
#
Propiedades
Métodos
Lock()
#
Unlock()
#
Takes it when no other thread holds it: whether it did.
WaitOne()
#
WaitOne and ReleaseMutex: the same as Lock and Unlock.
ReleaseMutex()
#
Run(func body)
#
Runs body holding the lock.
Dispose()
#
Frees it (it must not be used after).
override destroy()
#
Miembros privados e internos (6)
Miembros heredados (4)
GetTypemétodo de AnyUndescribedmétodo de AnyToStringmétodo de AnyHashmétodo de Any
Código fuente
Mutex en Antril, descompilado a partir de su Larva HIR.
// System.Threading.Mutex — System.Core (decompiled from Larva HIR)
into namespace System.Threading
{
/// A lock: one thread at a time between Lock and Unlock (not re-entrant: a thread that holds it must not lock it again).
/// `Run(() => …)` holds it around a function. Dispose frees its handle (an undisposed one is freed when the object dies).
public class Mutex
{
i64 handle;
static extern("mutex.new") New() : i64;
static extern("mutex.lock") LockNative(i64 handle);
static extern("mutex.trylock") TryNative(i64 handle) : i64;
static extern("mutex.unlock") UnlockNative(i64 handle);
static extern("mutex.free") Free(i64 handle);
public:
new()
{
handle = New();
}
Lock()
{
LockNative(handle);
}
Unlock()
{
UnlockNative(handle);
}
/// Takes it when no other thread holds it: whether it did.
TryLock() : bool => TryNative(handle) != 0;
/// WaitOne and ReleaseMutex: the same as Lock and Unlock.
WaitOne()
{
LockNative(handle);
}
ReleaseMutex()
{
UnlockNative(handle);
}
/// Runs `body` holding the lock.
Run(func body)
{
LockNative(handle);
try
{
body();
}
finally
{
UnlockNative(handle);
}
}
/// Frees it (it must not be used after).
Dispose()
{
if (handle != 0)
Free(handle);
handle = 0;
}
override destroy()
{
try
{
Dispose();
}
finally
{
base.destroy();
}
}
/// The handle (for Condition).
i64 Handle
{
get => handle;
}
}
}