Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make sure that exactly one thread will do something?

I have multiple threads which add items to a lock-free queue.
The items are then processed by another thread.

In the producer threads, I need to kick off the consumer thread, but only if it's not already running or kicked off.

Specifically:

public void BeginInvoke(Action method)
{
    //This runs on multiple background threads
    pendingActions.Enqueue(method);
    if (ProcessQueue hasn't been posted)
        uiContext.Post(ProcessQueue, null);
}
private void ProcessQueue(object unused)
{
    //This runs on the UI thread.
    Action current;
    while (pendingActions.TryDequeue(out current))
        current();
}

I'm using .Net 3.5, not 4.0. :(

like image 725
SLaks Avatar asked Jun 23 '11 15:06

SLaks


3 Answers

The easiest way is to use Semaphore. It will have a count of queue size.

like image 95
Andrey Avatar answered Oct 01 '22 22:10

Andrey


I created the following class to do this:

///<summary>Ensures that a block of code is only executed once at a time.</summary>
class Valve
{
    int isEntered;  //0 means false; 1 true

    ///<summary>Tries to enter the valve.</summary>
    ///<returns>True if no other thread is in the valve; false if the valve has already been entered.</returns>
    public bool TryEnter()
    {
        if (Interlocked.CompareExchange(ref isEntered, 1, 0) == 0)
            return true;
        return false;
    }

    ///<summary>Allows the valve to be entered again.</summary>
    public void Exit()
    {
        Debug.Assert(isEntered == 1);
        isEntered = 0;
    }
}

I use it like this:

readonly Valve valve = new Valve();
public void BeginInvoke(Action method)
{
    pendingActions.Enqueue(method);
    if (valve.TryEnter())
        uiContext.Post(ProcessQueue, null);
}
private void ProcessQueue(object unused)
{
    //This runs on the UI thread.
    Action current;
    while (pendingActions.TryDequeue(out current))
        current();
    valve.Exit();
}

Is this pattern safe?
Is there a better way to do this?
Is there a more correct name for the class?

like image 32
SLaks Avatar answered Oct 01 '22 21:10

SLaks


Does this work for you?

volatile int running;  //not a boolean to allow ProcessQueue to be reentrant.

private void ProcessQueue(object unused)
{
    do
    {
        ++running;
        Action current;
        while (pendingActions.TryDequeue(out current))
            current();

        --running;
    }
    while (pendingActions.Count != 0);
} 

public void BeginInvoke(Action method) 
{     
    pendingActions.Enqueue(method);
    if (running != 0)
        uiContext.Post(ProcessQueue, null); 
} 
like image 42
jyoung Avatar answered Oct 01 '22 20:10

jyoung