You are here: Client-Server > Concurrency Control > Semaphores

Semaphores

db4o semaphores are named flags that can only be owned by one object container at one time. They are supplied to be used as locks for exclusive access to code blocks in applications and to signal states from one client to the server and to all other clients.

The naming of semaphores is up to the application. Any string can be used.

Semaphores are freed automatically when a client disconnects correctly or when a clients presence is no longer detected by the server, that constantly monitors all client connections.

// Grab the lock. Specify the name and a timeout in milliseconds
bool hasLock = container.Ext().SetSemaphore("LockName", 1000);
try
{
    // you need to check the lock
    if (hasLock)
    {
        Console.WriteLine("Could get lock");
    }
    else
    {
        Console.WriteLine("Couldn't get lock");
    }
}
finally
{
    // release the lock
    container.Ext().ReleaseSemaphore("LockName");
}
SemaphoreExamples.cs: Grab a semaphore
' Grab the lock. Specify the name and a timeout in milliseconds
Dim hasLock As Boolean = container.Ext().SetSemaphore("LockName", 1000)
Try
    ' you need to check the lock
    If hasLock Then
        Console.WriteLine("Could get lock")
    Else
        Console.WriteLine("Couldn't get lock")
    End If
Finally
    ' release the lock
    container.Ext().ReleaseSemaphore("LockName")
End Try
SemaphoreExamples.vb: Grab a semaphore

Building Block

Semaphores are a low level building block. Usually they are not used directly. Instead you can build the abstractions you need with semaphores. For example you can build object-locking, critical sections etc with semaphores.