how to access the underlying default concurrent queue of a blocking collection

后端 未结 3 1253
广开言路
广开言路 2020-12-11 03:05

I have multiple producers and a single consumer. However if there is something in the queue that is not yet consumed a producer should not queue it again. (unique no duplica

3条回答
  •  旧巷少年郎
    2020-12-11 03:40

    I would suggest implementing your operations with lock so that you don't read and write the item in a way that corrupts it, making them atomic. For example, with any IEnumerable:

    object bcLocker = new object();
    
    // ...
    
    lock (bcLocker)
    {
        bool foundTheItem = false;
        foreach (someClass nextItem in myBlockingColl)
        {
            if (nextItem.Equals(item))
            {
                foundTheItem = true;
                break;
            }
        }
        if (foundTheItem == false)
        {
            // Add here
        }
    }
    

提交回复
热议问题