SignalAndWait for lock-context

I have a manager class that produces tasks for a threadpool, and each thread is supposed to do a call back once they are finished.

I use locks to handle variables and fields, and signals to handle interthread communications. What I'm looking for is a way of exiting the current lock() and wait for a signal atomically, something like SignalAndWait, but for locks().

The code looks something like this:

// ... part of the scheduler
foreach(WorkTask task in worktasks)
{
   lock(_syncObj)
   {
      new Job(task, myCallback); // creates a thread
      instanceCount++;
      while(instanceCount > _maxConcurrentTasks)
          _resetEvent.WaitOne(Timeout.Infinite);
   }
}

// .. the callback

void myCallback()
{
    lock(_syncObj)
    {
        instanceCount--;
        _resetEvent.Set();
    }
}

The problem here is that .WaitOne() doesn't exit the lock(), so any thread doing a callback will dead-lock.

I had high hopes for WaitOne(Int32, bool exitContext), but that context seems to be about remoting and stuff rather than synchronisations.

Answers


Is there any reason for using the event primitives rather than Monitor.Wait/Pulse/PulseAll? Monitor.Wait atomically releases the lock and waits, reacquiring the lock before it returns. See my threading article for more details.


Need Your Help

Advanced Breadcrumbs disabled my drop down menu

jquery joomla module

My knowledge of jQuery is limited, but I believe I'm experiencing a jQuery conflict. I'm using the Mega Menu as my navigation menu at the top of the page and the problem I'm having is that my drop ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.