By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

Semaphore wrapper class

P: n/a
I tried using a Semaphore class (have included the full listing
reproduced from article Figure 1 at the end of this email) in .NET 1.1.
It works fine most of the time, however, I see the following error
occassionally:
08/28/2005 17:32:42.82, Verbose, QueryObject.MergeSubQuery, [0x88]
Exception:
Too many posts were made to a semaphore in mscorlib.
Server stack trace:
at Symantec.MSS.Semaphore.ReleaseOne()
at Symantec.MSS.Data.SqlClient.QueryObject.MergeSubQu ery(IAsyncResult
iar)
at
System.Runtime.Remoting.Messaging.AsyncResult.Sync ProcessMessage(IMessage
msg)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .AsyncProcessMessage(IMessage
msg, IMessageSink replySink)

Calls to WaitOne and ReleaseOne match. Initial count and maximum count
are 5. I have many threads (34) that call WaitOne and ReleaseOne
rapidly, so I am wondering if there is some synchronization issue that
I am missing.

I am unable to contact the author Stephen Toub to ask him why this is
happening and I would very much appreciate help.
Link to article:
http://msdn.microsoft.com/msdnmag/is...12/NETMatters/
public sealed class Semaphore : WaitHandle
{
public Semaphore() : this(1, 1) {}

public Semaphore(int initialCount, int maximumCount)
{
if (initialCount < 0 || initialCount > maximumCount)
throw new ArgumentOutOfRangeException("initialCount");
if (maximumCount < 1)
throw new ArgumentOutOfRangeException("maximumCount");
IntPtr h = CreateSemaphore(
IntPtr.Zero, initialCount, maximumCount, null);
if (h == WaitHandle.InvalidHandle || h == IntPtr.Zero)
throw new Win32Exception();
Handle = h;
}

public void ReleaseOne()
{
int previousCount;
if (!ReleaseSemaphore(Handle, 1, out previousCount))
throw new Win32Exception();
}

[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr CreateSemaphore(
IntPtr lpSemaphoreAttributes, int lInitialCount,
int lMaximumCount, string lpName);

[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool ReleaseSemaphore(
IntPtr hSemaphore, int lReleaseCount, out int lpPreviousCount);
}

Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Have you tried my Dijkstra semaphore in C#?
http://www.codeproject.com/csharp/Di...gSemaphore.asp

--
William Stacey [MVP]

"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I tried using a Semaphore class (have included the full listing
reproduced from article Figure 1 at the end of this email) in .NET 1.1.
It works fine most of the time, however, I see the following error
occassionally:
08/28/2005 17:32:42.82, Verbose, QueryObject.MergeSubQuery, [0x88]
Exception:
Too many posts were made to a semaphore in mscorlib.
Server stack trace:
at Symantec.MSS.Semaphore.ReleaseOne()
at Symantec.MSS.Data.SqlClient.QueryObject.MergeSubQu ery(IAsyncResult
iar)
at
System.Runtime.Remoting.Messaging.AsyncResult.Sync ProcessMessage(IMessage
msg)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .AsyncProcessMessage(IMessage
msg, IMessageSink replySink)

Calls to WaitOne and ReleaseOne match. Initial count and maximum count
are 5. I have many threads (34) that call WaitOne and ReleaseOne
rapidly, so I am wondering if there is some synchronization issue that
I am missing.

I am unable to contact the author Stephen Toub to ask him why this is
happening and I would very much appreciate help.
Link to article:
http://msdn.microsoft.com/msdnmag/is...12/NETMatters/
public sealed class Semaphore : WaitHandle
{
public Semaphore() : this(1, 1) {}

public Semaphore(int initialCount, int maximumCount)
{
if (initialCount < 0 || initialCount > maximumCount)
throw new ArgumentOutOfRangeException("initialCount");
if (maximumCount < 1)
throw new ArgumentOutOfRangeException("maximumCount");
IntPtr h = CreateSemaphore(
IntPtr.Zero, initialCount, maximumCount, null);
if (h == WaitHandle.InvalidHandle || h == IntPtr.Zero)
throw new Win32Exception();
Handle = h;
}

public void ReleaseOne()
{
int previousCount;
if (!ReleaseSemaphore(Handle, 1, out previousCount))
throw new Win32Exception();
}

[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr CreateSemaphore(
IntPtr lpSemaphoreAttributes, int lInitialCount,
int lMaximumCount, string lpName);

[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool ReleaseSemaphore(
IntPtr hSemaphore, int lReleaseCount, out int lpPreviousCount);
}

Nov 17 '05 #2

P: n/a
Hard to tell what's going on without seeing any code that uses this class.

Willy.
"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I tried using a Semaphore class (have included the full listing
reproduced from article Figure 1 at the end of this email) in .NET 1.1.
It works fine most of the time, however, I see the following error
occassionally:
08/28/2005 17:32:42.82, Verbose, QueryObject.MergeSubQuery, [0x88]
Exception:
Too many posts were made to a semaphore in mscorlib.
Server stack trace:
at Symantec.MSS.Semaphore.ReleaseOne()
at Symantec.MSS.Data.SqlClient.QueryObject.MergeSubQu ery(IAsyncResult
iar)
at
System.Runtime.Remoting.Messaging.AsyncResult.Sync ProcessMessage(IMessage
msg)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .AsyncProcessMessage(IMessage
msg, IMessageSink replySink)

Calls to WaitOne and ReleaseOne match. Initial count and maximum count
are 5. I have many threads (34) that call WaitOne and ReleaseOne
rapidly, so I am wondering if there is some synchronization issue that
I am missing.

I am unable to contact the author Stephen Toub to ask him why this is
happening and I would very much appreciate help.
Link to article:
http://msdn.microsoft.com/msdnmag/is...12/NETMatters/
public sealed class Semaphore : WaitHandle
{
public Semaphore() : this(1, 1) {}

public Semaphore(int initialCount, int maximumCount)
{
if (initialCount < 0 || initialCount > maximumCount)
throw new ArgumentOutOfRangeException("initialCount");
if (maximumCount < 1)
throw new ArgumentOutOfRangeException("maximumCount");
IntPtr h = CreateSemaphore(
IntPtr.Zero, initialCount, maximumCount, null);
if (h == WaitHandle.InvalidHandle || h == IntPtr.Zero)
throw new Win32Exception();
Handle = h;
}

public void ReleaseOne()
{
int previousCount;
if (!ReleaseSemaphore(Handle, 1, out previousCount))
throw new Win32Exception();
}

[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr CreateSemaphore(
IntPtr lpSemaphoreAttributes, int lInitialCount,
int lMaximumCount, string lpName);

[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool ReleaseSemaphore(
IntPtr hSemaphore, int lReleaseCount, out int lpPreviousCount);
}

Nov 17 '05 #3

P: n/a
I found the mistake after building a smaller prototype but I still have
a question. The semaphore class was fine but my callback completion
function was being called twice sometimes but not always.

I saw your earlier post about calling EndInvoke on the same delegate
instance on which BeginInvoke was called, so I am hoping you will be
able to help.

My simplified incorrect code:
SubQueryDelegate e = new SubQueryDelegate(subQuery.Execute);
AsyncCallback callback = new AsyncCallback(MergeSubQuery);
e.BeginInvoke(callback, (object) subQuery); // NOTE: not passing
delegate as state

// Routine MergeSubQuery
SubQueryDelegate e = (SubQueryDelegate) ar.AsyncDelegate;
// some stuff

e.EndInvoke()

So, I am trying to get my delegate from AsyncResult - is this code
incorrect? Since I am using state object to pass a "SubQuery" instance,
can you suggest an alternate method to pass the delegate to the
callback?

Thanks

Nov 17 '05 #4

P: n/a
Thank you for responding quickly. I just found out that my Semaphore
class was fine but I had problems with using Async delegates correctly.
I just replied to Willy Denoyette with an accurate description of the
real problem.

Nov 17 '05 #5

P: n/a
Cool. I suspect mine will be slightly faster as no pinvoke is required and
the monitor is managed, however, glad you found it.

--
William Stacey [MVP]

"marvind" <ma********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Thank you for responding quickly. I just found out that my Semaphore
class was fine but I had problems with using Async delegates correctly.
I just replied to Willy Denoyette with an accurate description of the
real problem.

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.