Skip to content

CPU 6. Locks

Phillip Allen Lane edited this page Nov 27, 2023 · 1 revision

DotMP provides a locking API. For performance, it's advised to use the .NET lock keyword. However, this API is provided for those who want a locking API more similar to that which is used in OpenMP.

To create a lock, you can instantiate a DotMP.Lock object:

DotMP.Lock lck = new DotMP.Lock();

There are several methods you can call on a lock:

Obtaining a Lock

If you call:

lck.Set();

The current thread is halted until the lock is obtained, at which point execution proceeds as normal.

Freeing a Lock

If you call:

lck.Unset();

The current lock is freed for other threads to use.

Testing a Lock

If you call:

bool success = lck.Test();

This attempts to obtain a lock without blocking the thread. If the thread was successful in obtaining the lock, Test() returns true. If the lock has already been obtained by another thread, Test() returns false. Again, in neither case is the current thread blocked.