多线程多生产者和消费者线程将不会同步BlockingCollection竞争条件

时间:2013-01-28 17:54:20

标签: c# multithreading race-condition producer-consumer blockingcollection

我有多个生产者和多个消费者。我的共享资源是BlockingCollection。但是,只有我有一个消费者时,我的代码才有效。我知道这是一种竞争条件,因为每次运行代码时输出都不同。

我认为BlockingCollection将负责所有的同步等,但事实并非如此。

如何在所有生产者和消费者之间同步我的共享资源呢?

这是我的代码:

/// <summary>
/// PURE PRODUCER TYPE
/// </summary>
class Caller
{

    private BlockingCollection<Call> incommingCalls;

    public Caller(BlockingCollection<Call> calls)
    {
        incommingCalls = calls;
        //start the producer thread
        Thread thread = new Thread(new ThreadStart(placeCall));
        thread.Start();
    }

    public void placeCall()
    {
            incommingCalls.Add(myCall);
    }

}



/// <summary>
/// CONSUMER
/// </summary>
class Fresher : Employee
{

    private BlockingCollection<Call> calls;

    public Fresher(BlockingCollection<Call> incalls)
    {
        calls = incalls;
        Thread thread = new Thread(new ThreadStart(HandleCalls));
        thread.Start();

    }

    /// <summary>
    /// 
    /// </summary>
    public void HandleCalls()
    {
        while (!incommingCalls.IsCompleted)
        {
            Call item;
            if (incommingCalls.TryTake(out item, 100000))
            {
                //do something with the call

            } //else do nothing - just wait

        }


}






/// <summary>
/// 
/// </summary>
class CallCenter
{

    private BlockingCollection<Call> fresherCalls;


    private List<Caller> myCallers;
    private List<Employee> myFreshers;


    public CallCenter() 
    {
        //initial incomming calls to the fresher queue
        fresherCalls = new BlockingCollection<Call>();

        myFreshers = new List<Employee>();
        myCallers = new List<Caller>();

        generate_freshers();

        //generate to start the producer
        generate_callers();

    }

    /// <summary>
    /// 
    /// </summary>
    private void generate_freshers() 
    {
        for (int i = 0; i < 1; i++ )
        {
            myFreshers.Add(new Fresher(fresherCalls, tlCalls, locker2));
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void generate_callers() 
    {
        for (int i = 0; i < 20; i++ )
        {
            myCallers.Add(new Caller(fresherCalls, locker));
        }

    }
}

1 个答案:

答案 0 :(得分:3)

  

我知道这是竞争条件,因为每次运行代码时输出都不同。

这在多线程中很常见,并不一定是由于竞争条件(至少不是坏的)。多线程场景中的订单处理往往不具有确定性 - 这可能会改变输出。

话虽如此,使用BlockingCollection<T>,通常更容易将您的消费者编写为:

public void HandleCalls()
{
    foreach(var item in incommingCalls.GetConsumingEnumerable())
    {
        //do something with the call
    }
}

这将处理BlockingCollection<T>上任意数量的消费者的所有同步和检查。


编辑:如果您需要控制调度并实现某种形式的Round-Robin Scheduling,您可能需要查看TPL示例中的Parallel Extension Extras。它们提供RoundRobinTaskScheduler,可用于安排以可预测的方式工作的Task<T>个实例。