Who we are

We are the developers of Plastic SCM, a full version control stack (not a Git variant). We work on the strongest branching and merging you can find, and a core that doesn't cringe with huge binaries and repos. We also develop the GUIs, mergetools and everything needed to give you the full version control stack.

If you want to give it a try, download it from here.

We also code SemanticMerge, and the gmaster Git client.

Some C# coding: semaphores

Sunday, July 23, 2006 Pablo Santos 3 Comments

AFAIK there is no Semaphore implementation in .NET. Today I needed a semaphore to implement some of the new functionality of a new subsystem (more on this later this week).

I played a little bit with it and here it is the tiny semaphore C# code. I wonder if is correct...


class Semaphore
{
object mLock = new object();
long mCount = 0;

void Enter()
{
lock( mLock )
{
while( mCount == 0 )
{
Monitor.Wait(mLock);
}
--mCount;
}
}

void Release()
{
lock( mLock )
{
++mCount;
Monitor.Pulse(mLock);
}
}
}

Pablo Santos
I'm the CTO and Founder at Códice.
I've been leading Plastic SCM since 2005. My passion is helping teams work better through version control.
I had the opportunity to see teams from many different industries at work while I helped them improving their version control practices.
I really enjoy teaching (I've been a University professor for 6+ years) and sharing my experience in talks and articles.
And I love simple code. You can reach me at @psluaces.

3 comments:

  1. Actually, you might have found this by now but there is a Semaphore class on System.Threading namespace in addition to many other classes you can use if you need thread synchronization like ReaderWriterLock and Mutex.
    Your code is good though, I just thought I'd point it out.

    ReplyDelete
  2. Matheus,

    Is this Semaphore implementation available on .NET 1.1?

    ReplyDelete
  3. Actually only on .Net 3...
    thanks for the code, I think taht's exactly what I need.

    ReplyDelete