Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Semaphore object in .NET?

0 views
Skip to first unread message

tsao

unread,
Oct 5, 2001, 4:39:15 AM10/5/01
to
I can't find semaphore equivalent object in .NET framework
library. How can semaphore be implemented?
Thanks


Lee Johnson

unread,
Oct 7, 2001, 11:23:34 PM10/7/01
to
.NET is not Win32 and it currently doesn't directly support the semaphore
object. You may need to implement this functionality in your own way using
System.Threading.Monitor.

Some sample code may be helpful:
class Semaphore {

public Semaphore(int maxCount, int initialCount) {

_maxCount = maxCount;

_currentCount = currentCount;

}

public void Acquire() {

lock(this) {

while (_currentCount >= _maxCount) {

Monitor.Wait(this);

}

++_currentCount;

}

}

public void Release(int count) {

lock(this) {

_currentCount -= count;

Monitor.PulseAll(this);

}

}

private int _maxCount;

private int _currentCount;

}

---------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. Copyright 2001 Microsoft Corporation. All
rights reserved.

0 new messages