public class Semaphore

Summary

A classic counting semaphore.

Remarks

The .NET framework does not introduce a counting semaphore until framework release 2.0. Consequently, we implement one here, for the benefit of .NET 1.1.

Property Summary

Flags Type Name Summary
public int Value (r)

Retrieve the current semaphore value.

Constructor Summary

Flags Name Summary
public Semaphore(int initialCount)

Create a Semaphore, with its counter initialized to the value passed in.

public Semaphore()

Create a Semaphore, with its counter initialized to 1.

Method Summary

Flags Name Summary
public void Release()

Release a single resource, incrementing the count by one.

public bool TryWait()

Acquire a single resource, decrementing the count by one and returning true, if a resource is available; otherwise, return false immediately.

public void Wait()

Acquire a single resource, decrementing the count by one.

Property Detail

public int Value (r)

Summary

Retrieve the current semaphore value.

Remarks

Consider carefully whether this property is actually what you want - usually, using this property is wrong, and either Wait() or TryWait() is the correct choice.

Constructor Detail

Semaphore

public Semaphore(int initialCount)

Parameters
Name Type
initialCount int

Summary

Create a Semaphore, with its counter initialized to the value passed in.

Semaphore

public Semaphore()

Summary

Create a Semaphore, with its counter initialized to 1.

Method Detail

Release

public void Release()

Flags public
Return type void

Summary

Release a single resource, incrementing the count by one.

TryWait

public bool TryWait()

Flags public
Return type bool

Summary

Acquire a single resource, decrementing the count by one and returning true, if a resource is available; otherwise, return false immediately.

Wait

public void Wait()

Flags public
Return type void

Summary

Acquire a single resource, decrementing the count by one.

Remarks

Not interruptable - will retry forever until a resource comes available.