Is this thread safe?

This is a discussion on Is this thread safe? within the C# Programming forums, part of the General Programming Boards category; Code: using System; using System.Collections.Generic; namespace core.Extensions { class ConcurrentList<T> { private List<T> Items { get; set; } private object ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209

    Is this thread safe?

    Code:
    using System;
    using System.Collections.Generic;
    
    namespace core.Extensions
    {
        class ConcurrentList<T>
        {
            private List<T> Items { get; set; }
            private object Locker { get; set; }
    
            public ConcurrentList()
            {
                this.Items = new List<T>();
                this.Locker = new object();
            }
    
            public void Add(T obj) // called from GUI thread
            {
                lock (this.Locker)
                    this.Items.Add(obj);
            }
    
            public T TakeFirst(Predicate<T> predicate) // called from GUI thread
            {
                T obj = default(T);
    
                lock (this.Locker)
                {
                    int index = this.Items.FindIndex(predicate);
    
                    if (index > -1)
                    {
                        obj = this.Items[index];
                        this.Items.RemoveAt(index);
                    }
                }
    
                return obj;
            }
    
            public void ForEach(Action<T> action) // called from server thread
            {
                T[] copy;
    
                lock (this.Locker)
                    copy = this.Items.ToArray();
    
                foreach (T obj in copy)
                    action(obj);
            }
        }
    }
    Items are added and removed from the main GUI thread. The server's thread will periodically loop through the list items. I would have preferred to have used one of the System.Collections.Concurrent classes but they all seem to lack the ability to remove specific items, and instead always remove from the start or end of the collection, depending on whether it is LIFO or FIFO.

    Does my attempt at a thread safe list look to be thread safe? Would there be a better/more efficient way of tackling this problem?

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Nevermind. ConcurrentDictionary seems to be what I need.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    To answer your question anyway, no it's not thread safe. Your ForEach method, if operating on objects, may reference an object that is no longer part of the collection while it is running.

    And I would think ConcurrentBag would do what you are doing here. You'd use TryTake instead of RemoveAt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. thread-safe singleton?
    By bling in forum C++ Programming
    Replies: 12
    Last Post: 10-01-2008, 09:49 PM
  2. Safe Thread Queue
    By mauricio.sch in forum C++ Programming
    Replies: 5
    Last Post: 01-29-2008, 05:19 PM
  3. Is DrawDibDraw thread safe?
    By vart in forum Windows Programming
    Replies: 0
    Last Post: 11-14-2006, 10:53 PM
  4. Is this thread safe
    By Bru5 in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2006, 05:34 PM
  5. STL size thread safe
    By vasanth in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 10:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21