Click here to Skip to main content
15,891,657 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# features Pin
OriginalGriff25-May-23 18:33
mveOriginalGriff25-May-23 18:33 
GeneralRe: C# features Pin
Calin Negru26-May-23 0:09
Calin Negru26-May-23 0:09 
GeneralRe: C# features Pin
Gerry Schmitz26-May-23 3:27
mveGerry Schmitz26-May-23 3:27 
GeneralRe: C# features Pin
Calin Negru26-May-23 22:11
Calin Negru26-May-23 22:11 
GeneralRe: C# features Pin
Gerry Schmitz27-May-23 2:54
mveGerry Schmitz27-May-23 2:54 
GeneralRe: C# features Pin
jschell27-May-23 9:29
jschell27-May-23 9:29 
GeneralRe: C# features Pin
Gerry Schmitz27-May-23 13:28
mveGerry Schmitz27-May-23 13:28 
GeneralRe: C# features Pin
jschell29-May-23 6:02
jschell29-May-23 6:02 
GeneralRe: C# features Pin
Calin Negru28-May-23 4:16
Calin Negru28-May-23 4:16 
GeneralRe: C# features Pin
Gerry Schmitz28-May-23 4:52
mveGerry Schmitz28-May-23 4:52 
GeneralRe: C# features Pin
Calin Negru29-May-23 2:39
Calin Negru29-May-23 2:39 
GeneralRe: C# features Pin
PIEBALDconsult26-May-23 4:57
mvePIEBALDconsult26-May-23 4:57 
GeneralRe: C# features Pin
OriginalGriff26-May-23 11:36
mveOriginalGriff26-May-23 11:36 
GeneralRe: C# features Pin
PIEBALDconsult26-May-23 11:42
mvePIEBALDconsult26-May-23 11:42 
AnswerRe: C# features Pin
Richard MacCutchan26-May-23 1:04
mveRichard MacCutchan26-May-23 1:04 
GeneralRe: C# features Pin
Calin Negru26-May-23 21:37
Calin Negru26-May-23 21:37 
AnswerRe: C# features Pin
BillWoodruff26-May-23 19:42
professionalBillWoodruff26-May-23 19:42 
GeneralRe: C# features Pin
Calin Negru29-May-23 6:05
Calin Negru29-May-23 6:05 
QuestionGoogle API Authentication in a WPF App Pin
Kevin Marois22-May-23 14:04
professionalKevin Marois22-May-23 14:04 
AnswerRe: Google API Authentication in a WPF App Pin
Dave Kreskowiak22-May-23 18:54
mveDave Kreskowiak22-May-23 18:54 
QuestionConcurrentList<T> Pin
Richard Andrew x6421-May-23 8:40
professionalRichard Andrew x6421-May-23 8:40 
Microsoft provides a group of thread-safe collections, but conspicuously does not provide a thread-safe List<T>. What is the reason? Is it not feasible to make a thread-safe version of a List<T>?

I need an ordered, sortable collection that is thread-safe.

I might get flamed, but I'm about to copy and paste some code I found on the internet. On stack exchange, I found the following post:

https://codereview.stackexchange.com/questions/62033/creating-a-thread-safe-list-using-a-lock-object[^]

To me, it looks like it would work and be thread-safe.

I'm wondering if some more experienced and respected members can tell me if this code looks like it might indeed be thread safe. The key bit about this code is that it uses callbacks to be able to keep the collection locked even while enumerating it or performing some other types of access.

I have reproduced the code below for convenience:

(If you consider this post inappropriate due to its length, please let me know, and I won't do it again.)

public class ConcurrentList<T> : IList<T>
{
    #region Fields

    private IList<T> _internalList;

    private readonly object lockObject = new object();

    #endregion

    #region ctor

    public ConcurrentList()
    {
        _internalList = new List<T>();
    }

    public ConcurrentList(int capacity)
    {
        _internalList = new List<T>(capacity);
    }

    public ConcurrentList(IEnumerable<T> list)
    {
        _internalList = list.ToList();
    }

    #endregion

    public T this[int index]
    {
        get
        {
            return LockInternalListAndGet(l => l[index]);
        }

        set
        {
            LockInternalListAndCommand(l => l[index] = value);
        }
    }

    public int Count
    {
        get
        {
            return LockInternalListAndQuery(l => l.Count());
        }
    }

    public bool IsReadOnly => false;

    public void Add(T item)
    {
        LockInternalListAndCommand(l => l.Add(item));
    }

    public void Clear()
    {
        LockInternalListAndCommand(l => l.Clear());
    }

    public bool Contains(T item)
    {
        return LockInternalListAndQuery(l => l.Contains(item));
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        LockInternalListAndCommand(l => l.CopyTo(array, arrayIndex));
    }

    public IEnumerator<T> GetEnumerator()
    {
        return LockInternalListAndQuery(l => l.GetEnumerator());
    }

    public int IndexOf(T item)
    {
        return LockInternalListAndQuery(l => l.IndexOf(item));
    }

    public void Insert(int index, T item)
    {
        LockInternalListAndCommand(l => l.Insert(index, item));
    }

    public bool Remove(T item)
    {
        return LockInternalListAndQuery(l => l.Remove(item));
    }

    public void RemoveAt(int index)
    {
        LockInternalListAndCommand(l => l.RemoveAt(index));
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return LockInternalListAndQuery(l => l.GetEnumerator());

    }

    #region Utilities

    protected virtual  void LockInternalListAndCommand(Action<IList<T>> action)
    {
        lock (lockObject)
        {
            action(_internalList);
        }
    }

    protected virtual T LockInternalListAndGet(Func<IList<T>, T> func)
    {
        lock (lockObject)
        {
            return func(_internalList);
        }
    }

    protected virtual TObject LockInternalListAndQuery<TObject>(Func<IList<T>, TObject> query)
    {
        lock (lockObject)
        {
            return query(_internalList);
        }
    }

    #endregion
}




The difficult we do right away...
...the impossible takes slightly longer.


modified 21-May-23 16:33pm.

AnswerRe: ConcurrentList<T> Pin
Gerry Schmitz21-May-23 8:50
mveGerry Schmitz21-May-23 8:50 
GeneralRe: ConcurrentList<T> Pin
Richard Andrew x6421-May-23 10:32
professionalRichard Andrew x6421-May-23 10:32 
GeneralRe: ConcurrentList<T> Pin
Gerry Schmitz22-May-23 12:01
mveGerry Schmitz22-May-23 12:01 
GeneralRe: ConcurrentList<T> Pin
BillWoodruff22-May-23 11:38
professionalBillWoodruff22-May-23 11:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.