• Performance
  • API Documentation
  • Source Code
Show / Hide Table of Contents
  • InSync
    • AsyncSynchronized
    • AsyncSynchronized<T>
    • GuardedValue<T>
    • IAsyncSynchronized<T>
    • IBareAsyncLock
    • IBareAsyncLock<T>
    • IBareLock
    • IBareLock<T>
    • ISynchronized<T>
    • LockException
    • MultiSync
    • ReaderSynchronized<TRead>
    • ReaderWriterSynchronized<T>
    • ReaderWriterSynchronized<TWrite, TRead>
    • ReaderWriterSynchronizedFactory
    • Synchronized
    • Synchronized<T>
    • TimingMethod
    • UnlockException
    • UpgradeableReaderSynchronized<TRead>
    • ValueContainer<T>

Class MultiSync

Provides easy ways to acquire multiple locks without deadlocks. It does not require any setup nor lock organizations. Fairness is thread-based and provided by the OS because is used. Livelock may occur for a very short period under high contention. In such case, CPU power is wasted.

It uses the smart and polite method described in https://howardhinnant.github.io/dining_philosophers.html#Polite. Basically, it tries to acquire the locks one by one. If an acquisition fails, it releases all acquired locks. Before a blocking retry of the last acquisition, it yields to let other threads to process first.

Inheritance
System.Object
MultiSync
Namespace: InSync
Assembly: InSync.dll
Syntax
public static class MultiSync : object

Methods

All(IReadOnlyList<IBareLock>)

Acquires the locks unorderly. See MultiSync for more details.

Declaration
public static GuardedValue<IReadOnlyList<object>> All(IReadOnlyList<IBareLock> locks)
Parameters
Type Name Description
IReadOnlyList<IBareLock> locks

The locks to acquire.

Returns
Type Description
GuardedValue<IReadOnlyList<System.Object>>

An containing the protected objects.

All(IReadOnlyList<IBareLock>, Int32, TimingMethod)

Acquires the locks unorderly. EnvironmentTick is good enough for general usages. This method detects an increase in remaining time and continue the counting from there.
Although EnvironmentTick uses which is 32-bits (representing 49.8 days), it is practically safe. The wrap around problem is only effective if an execution is suspended for 49.8 days subtracting the timeout, at least 24.9 days.
DateTime is affected by system clock changes. A clock may change several minutes for a low quality system in a network time synchronization. An user may change their system clock to an arbitrary time.
See MultiSync for more details.

Declaration
public static GuardedValue<IReadOnlyList<object>> All(IReadOnlyList<IBareLock> locks, int millisecondsTimeout, TimingMethod timingMethod)
Parameters
Type Name Description
IReadOnlyList<IBareLock> locks

The locks to acquire.

System.Int32 millisecondsTimeout

The number of milliseconds to wait. A negative value specifies an infinite wait.

TimingMethod timingMethod

The method of counting time. See TimingMethod for more details.

Returns
Type Description
GuardedValue<IReadOnlyList<System.Object>>

An containing the protected objects if all the locks are acquired; otherwise, null is returned.

All<T>(IReadOnlyList<IBareLock<T>>)

Acquires the locks in the list unorderly. See MultiSync for more details.

Declaration
public static GuardedValue<IReadOnlyList<T>> All<T>(IReadOnlyList<IBareLock<T>> locks)
    where T : class
Parameters
Type Name Description
IReadOnlyList<IBareLock<T>> locks

The locks to acquire.

Returns
Type Description
GuardedValue<IReadOnlyList<T>>

An containing the protected objects.

Type Parameters
Name Description
T

The type of the protected objects.

All<T>(IReadOnlyList<IBareLock<T>>, Int32, TimingMethod)

Acquires the locks in the list unorderly. EnvironmentTick is good enough for general usages. This method detects an increase in remaining time and continue the counting from there.
Although EnvironmentTick uses which is 32-bits (representing 49.8 days), it is practically safe. The wrap around problem is only effective if an execution is suspended for 49.8 days subtracting the timeout, at least 24.9 days.
DateTime is affected by system clock changes. A clock may change several minutes for a low quality system in a network time synchronization. An user may change their system clock to an arbitrary time.
See MultiSync for more details.

Declaration
public static GuardedValue<IReadOnlyList<T>> All<T>(IReadOnlyList<IBareLock<T>> locks, int millisecondsTimeout, TimingMethod timingMethod)
    where T : class
Parameters
Type Name Description
IReadOnlyList<IBareLock<T>> locks

The locks to acquire.

System.Int32 millisecondsTimeout

The number of milliseconds to wait. A negative value specifies an infinite wait.

TimingMethod timingMethod

The method of counting time. See TimingMethod for more details.

Returns
Type Description
GuardedValue<IReadOnlyList<T>>

An containing the protected objects if all the locks are acquired; otherwise, null is returned.

Type Parameters
Name Description
T

All<T1, T2>(IBareLock<T1>, IBareLock<T2>)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static GuardedValue<Tuple<T1, T2>> All<T1, T2>(IBareLock<T1> lock1, IBareLock<T2> lock2)
    where T1 : class where T2 : class
Parameters
Type Name Description
IBareLock<T1> lock1

The first lock to acquire.

IBareLock<T2> lock2

The second lock to acquire.

Returns
Type Description
GuardedValue<Tuple<T1, T2>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

All<T1, T2>(IBareLock<T1>, IBareLock<T2>, Int32, TimingMethod)

Acquires the locks unorderly. The protected objects are returned in a tuple. See All(IReadOnlyList<IBareLock>, Int32, TimingMethod) for more details.

Declaration
public static GuardedValue<Tuple<T1, T2>> All<T1, T2>(IBareLock<T1> lock1, IBareLock<T2> lock2, int millisecondsTimeout, TimingMethod timingMethod)
    where T1 : class where T2 : class
Parameters
Type Name Description
IBareLock<T1> lock1

The first lock to acquire.

IBareLock<T2> lock2

The second lock to acquire.

System.Int32 millisecondsTimeout

The number of milliseconds to wait. A negative value specifies an infinite wait.

TimingMethod timingMethod

The method of counting time. See TimingMethod for more details.

Returns
Type Description
GuardedValue<Tuple<T1, T2>>

An containing the protected objects if all the locks are acquired; otherwise, null is returned.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

All<T1, T2, T3>(IBareLock<T1>, IBareLock<T2>, IBareLock<T3>)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static GuardedValue<Tuple<T1, T2, T3>> All<T1, T2, T3>(IBareLock<T1> lock1, IBareLock<T2> lock2, IBareLock<T3> lock3)
    where T1 : class where T2 : class where T3 : class
Parameters
Type Name Description
IBareLock<T1> lock1

The first lock to acquire.

IBareLock<T2> lock2

The second lock to acquire.

IBareLock<T3> lock3

The third lock to acquire.

Returns
Type Description
GuardedValue<Tuple<T1, T2, T3>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

All<T1, T2, T3>(IBareLock<T1>, IBareLock<T2>, IBareLock<T3>, Int32, TimingMethod)

Acquires the locks unorderly. The protected objects are returned in a tuple. See All(IReadOnlyList<IBareLock>, Int32, TimingMethod) for more details.

Declaration
public static GuardedValue<Tuple<T1, T2, T3>> All<T1, T2, T3>(IBareLock<T1> lock1, IBareLock<T2> lock2, IBareLock<T3> lock3, int millisecondsTimeout, TimingMethod timingMethod)
    where T1 : class where T2 : class where T3 : class
Parameters
Type Name Description
IBareLock<T1> lock1

The first lock to acquire.

IBareLock<T2> lock2

The second lock to acquire.

IBareLock<T3> lock3

The third lock to acquire.

System.Int32 millisecondsTimeout

The number of milliseconds to wait. A negative value specifies an infinite wait.

TimingMethod timingMethod

The method of counting time. See TimingMethod for more details.

Returns
Type Description
GuardedValue<Tuple<T1, T2, T3>>

An containing the protected objects if all the locks are acquired; otherwise, null is returned.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

All<T1, T2, T3, T4>(IBareLock<T1>, IBareLock<T2>, IBareLock<T3>, IBareLock<T4>)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static GuardedValue<Tuple<T1, T2, T3, T4>> All<T1, T2, T3, T4>(IBareLock<T1> lock1, IBareLock<T2> lock2, IBareLock<T3> lock3, IBareLock<T4> lock4)
    where T1 : class where T2 : class where T3 : class where T4 : class
Parameters
Type Name Description
IBareLock<T1> lock1

The first lock to acquire.

IBareLock<T2> lock2

The second lock to acquire.

IBareLock<T3> lock3

The third lock to acquire.

IBareLock<T4> lock4

The forth lock to acquire.

Returns
Type Description
GuardedValue<Tuple<T1, T2, T3, T4>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

T4

The type of the forth protected object.

All<T1, T2, T3, T4>(IBareLock<T1>, IBareLock<T2>, IBareLock<T3>, IBareLock<T4>, Int32, TimingMethod)

Acquires the locks unorderly. The protected objects are returned in a tuple. See All(IReadOnlyList<IBareLock>, Int32, TimingMethod) for more details.

Declaration
public static GuardedValue<Tuple<T1, T2, T3, T4>> All<T1, T2, T3, T4>(IBareLock<T1> lock1, IBareLock<T2> lock2, IBareLock<T3> lock3, IBareLock<T4> lock4, int millisecondsTimeout, TimingMethod timingMethod)
    where T1 : class where T2 : class where T3 : class where T4 : class
Parameters
Type Name Description
IBareLock<T1> lock1

The first lock to acquire.

IBareLock<T2> lock2

The second lock to acquire.

IBareLock<T3> lock3

The third lock to acquire.

IBareLock<T4> lock4

The forth lock to acquire.

System.Int32 millisecondsTimeout

The number of milliseconds to wait. A negative value specifies an infinite wait.

TimingMethod timingMethod

The method of counting time. See TimingMethod for more details.

Returns
Type Description
GuardedValue<Tuple<T1, T2, T3, T4>>

An containing the protected objects if all the locks are acquired; otherwise, null is returned.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

T4

The type of the forth protected object.

AllAsync(IReadOnlyList<IBareAsyncLock>)

Acquires the locks unorderly. See MultiSync for more details.

Declaration
public static Task<GuardedValue<IReadOnlyList<object>>> AllAsync(IReadOnlyList<IBareAsyncLock> locks)
Parameters
Type Name Description
IReadOnlyList<IBareAsyncLock> locks

The locks to acquire.

Returns
Type Description
Task<GuardedValue<IReadOnlyList<System.Object>>>

An containing the protected objects.

AllAsync(IReadOnlyList<IBareAsyncLock>, CancellationToken)

Acquires the locks unorderly. See MultiSync for more details.

Declaration
public static Task<GuardedValue<IReadOnlyList<object>>> AllAsync(IReadOnlyList<IBareAsyncLock> locks, CancellationToken cancellationToken)
Parameters
Type Name Description
IReadOnlyList<IBareAsyncLock> locks

The locks to acquire.

CancellationToken cancellationToken

A to observe while waiting for the tasks to complete.

Returns
Type Description
Task<GuardedValue<IReadOnlyList<System.Object>>>

An containing the protected objects.

AllAsync<T>(IReadOnlyList<IBareAsyncLock<T>>)

Acquires the locks in the list unorderly. See MultiSync for more details.

Declaration
public static Task<GuardedValue<IReadOnlyList<T>>> AllAsync<T>(IReadOnlyList<IBareAsyncLock<T>> locks)
    where T : class
Parameters
Type Name Description
IReadOnlyList<IBareAsyncLock<T>> locks

The locks to acquire.

Returns
Type Description
Task<GuardedValue<IReadOnlyList<T>>>

An containing the protected objects.

Type Parameters
Name Description
T

The type of the protected objects.

AllAsync<T>(IReadOnlyList<IBareAsyncLock<T>>, CancellationToken)

Acquires the locks in the list unorderly. See MultiSync for more details.

Declaration
public static Task<GuardedValue<IReadOnlyList<T>>> AllAsync<T>(IReadOnlyList<IBareAsyncLock<T>> locks, CancellationToken cancellationToken)
    where T : class
Parameters
Type Name Description
IReadOnlyList<IBareAsyncLock<T>> locks

The locks to acquire.

CancellationToken cancellationToken

A to observe while waiting for the tasks to complete.

Returns
Type Description
Task<GuardedValue<IReadOnlyList<T>>>

An containing the protected objects.

Type Parameters
Name Description
T

The type of the protected objects.

AllAsync<T1, T2>(IBareAsyncLock<T1>, IBareAsyncLock<T2>)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static Task<GuardedValue<Tuple<T1, T2>>> AllAsync<T1, T2>(IBareAsyncLock<T1> lock1, IBareAsyncLock<T2> lock2)
    where T1 : class where T2 : class
Parameters
Type Name Description
IBareAsyncLock<T1> lock1

The first lock to acquire.

IBareAsyncLock<T2> lock2

The second lock to acquire.

Returns
Type Description
Task<GuardedValue<Tuple<T1, T2>>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

AllAsync<T1, T2>(IBareAsyncLock<T1>, IBareAsyncLock<T2>, CancellationToken)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static Task<GuardedValue<Tuple<T1, T2>>> AllAsync<T1, T2>(IBareAsyncLock<T1> lock1, IBareAsyncLock<T2> lock2, CancellationToken cancellationToken)
    where T1 : class where T2 : class
Parameters
Type Name Description
IBareAsyncLock<T1> lock1

The first lock to acquire.

IBareAsyncLock<T2> lock2

The second lock to acquire.

CancellationToken cancellationToken

A to observe while waiting for the tasks to complete.

Returns
Type Description
Task<GuardedValue<Tuple<T1, T2>>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

AllAsync<T1, T2, T3>(IBareAsyncLock<T1>, IBareAsyncLock<T2>, IBareAsyncLock<T3>)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static Task<GuardedValue<Tuple<T1, T2, T3>>> AllAsync<T1, T2, T3>(IBareAsyncLock<T1> lock1, IBareAsyncLock<T2> lock2, IBareAsyncLock<T3> lock3)
    where T1 : class where T2 : class where T3 : class
Parameters
Type Name Description
IBareAsyncLock<T1> lock1

The first lock to acquire.

IBareAsyncLock<T2> lock2

The second lock to acquire.

IBareAsyncLock<T3> lock3

The third lock to acquire.

Returns
Type Description
Task<GuardedValue<Tuple<T1, T2, T3>>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

AllAsync<T1, T2, T3>(IBareAsyncLock<T1>, IBareAsyncLock<T2>, IBareAsyncLock<T3>, CancellationToken)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static Task<GuardedValue<Tuple<T1, T2, T3>>> AllAsync<T1, T2, T3>(IBareAsyncLock<T1> lock1, IBareAsyncLock<T2> lock2, IBareAsyncLock<T3> lock3, CancellationToken cancellationToken)
    where T1 : class where T2 : class where T3 : class
Parameters
Type Name Description
IBareAsyncLock<T1> lock1

The first lock to acquire.

IBareAsyncLock<T2> lock2

The second lock to acquire.

IBareAsyncLock<T3> lock3

The third lock to acquire.

CancellationToken cancellationToken

A to observe while waiting for the tasks to complete.

Returns
Type Description
Task<GuardedValue<Tuple<T1, T2, T3>>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

AllAsync<T1, T2, T3, T4>(IBareAsyncLock<T1>, IBareAsyncLock<T2>, IBareAsyncLock<T3>, IBareAsyncLock<T4>)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static Task<GuardedValue<Tuple<T1, T2, T3, T4>>> AllAsync<T1, T2, T3, T4>(IBareAsyncLock<T1> lock1, IBareAsyncLock<T2> lock2, IBareAsyncLock<T3> lock3, IBareAsyncLock<T4> lock4)
    where T1 : class where T2 : class where T3 : class where T4 : class
Parameters
Type Name Description
IBareAsyncLock<T1> lock1

The first lock to acquire.

IBareAsyncLock<T2> lock2

The second lock to acquire.

IBareAsyncLock<T3> lock3

The third lock to acquire.

IBareAsyncLock<T4> lock4

The forth lock to acquire.

Returns
Type Description
Task<GuardedValue<Tuple<T1, T2, T3, T4>>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

T4

The type of the forth protected object.

AllAsync<T1, T2, T3, T4>(IBareAsyncLock<T1>, IBareAsyncLock<T2>, IBareAsyncLock<T3>, IBareAsyncLock<T4>, CancellationToken)

Acquires the locks unorderly. The protected objects are returned in a tuple. See MultiSync for more details.

Declaration
public static Task<GuardedValue<Tuple<T1, T2, T3, T4>>> AllAsync<T1, T2, T3, T4>(IBareAsyncLock<T1> lock1, IBareAsyncLock<T2> lock2, IBareAsyncLock<T3> lock3, IBareAsyncLock<T4> lock4, CancellationToken cancellationToken)
    where T1 : class where T2 : class where T3 : class where T4 : class
Parameters
Type Name Description
IBareAsyncLock<T1> lock1

The first lock to acquire.

IBareAsyncLock<T2> lock2

The second lock to acquire.

IBareAsyncLock<T3> lock3

The third lock to acquire.

IBareAsyncLock<T4> lock4

The forth lock to acquire.

CancellationToken cancellationToken

A to observe while waiting for the tasks to complete.

Returns
Type Description
Task<GuardedValue<Tuple<T1, T2, T3, T4>>>

An containing the protected objects.

Type Parameters
Name Description
T1

The type of the first protected object.

T2

The type of the second protected object.

T3

The type of the third protected object.

T4

The type of the forth protected object.

In This Article
Back to top Generated by DocFX