locking - C# TPL Multiple List Enumerators One Modifier Without Starvation -


i have several threads created through tpl (probably not relevant). threads interact particular object enumerating on contained list without ever modifying anything. other threads add or remove items list. currently, have lock statement around code segments enumerate list , lock statements around code segments modify list.

i'm not experiencing performance problems that's that. however, realize more-efficient solution allow many concurrent enumerators , lock out else when modifying list. currently, 1 thread may enumerating list @ given time. future reference, pattern allows this?

important. there many solutions work fine in lot of cases won't work mine. in application, there chance barrage of readers never stop -- starving modifiers. i'm looking for:

enumerate 1 enumerate 2 concurrent 1 modify 1 request queued enumerate 3 request queued because of modify request enumerate 4 request queued modify 2 request queued enumerate 2 finishes enumerate 1 finishes modify 1 starts because in-progress @ time of request enumerators finished modify 1 finishes enumerate 3 starts because queued modify 1 finished enumerate 4 starts enumerate 3 finishes enumerate 4 finishes modify 2 starts ... 

i had implement concurrent list recently, , welcome try out if helps:

public class concurrentlist<t> : ilist<t>, ilist {     private readonly list<t> underlyinglist = new list<t>();     private readonly object syncroot = new object();     private readonly concurrentqueue<t> underlyingqueue;     private bool requiressync;     private bool isdirty;      public concurrentlist()     {         underlyingqueue = new concurrentqueue<t>();     }      public concurrentlist(ienumerable<t> items)     {         underlyingqueue = new concurrentqueue<t>(items);     }      private void updatelists()     {         if (!isdirty)             return;         lock (syncroot)         {             requiressync = true;             t temp;             while (underlyingqueue.trydequeue(out temp))                 underlyinglist.add(temp);             requiressync = false;         }     }      public ienumerator<t> getenumerator()     {         lock (syncroot)         {             updatelists();             return underlyinglist.tolist().getenumerator();         }     }      ienumerator ienumerable.getenumerator()     {         return getenumerator();     }      public void add(t item)     {         if (requiressync)             lock (syncroot)                 underlyingqueue.enqueue(item);         else             underlyingqueue.enqueue(item);         isdirty = true;     }      public int add(object value)     {         if (requiressync)             lock (syncroot)                 underlyingqueue.enqueue((t)value);         else             underlyingqueue.enqueue((t)value);         isdirty = true;         lock (syncroot)         {             updatelists();             return underlyinglist.indexof((t)value);         }     }      public bool contains(object value)     {         lock (syncroot)         {             updatelists();             return underlyinglist.contains((t)value);         }     }      public int indexof(object value)     {         lock (syncroot)         {             updatelists();             return underlyinglist.indexof((t)value);         }     }      public void insert(int index, object value)     {         lock (syncroot)         {             updatelists();             underlyinglist.insert(index, (t)value);         }     }      public void remove(object value)     {         lock (syncroot)         {             updatelists();             underlyinglist.remove((t)value);         }     }      public void removeat(int index)     {         lock (syncroot)         {             updatelists();             underlyinglist.removeat(index);         }     }      t ilist<t>.this[int index]     {                 {             lock (syncroot)             {                 updatelists();                 return underlyinglist[index];             }         }         set         {             lock (syncroot)             {                 updatelists();                 underlyinglist[index] = value;             }         }     }      object ilist.this[int index]     {         { return ((ilist<t>)this)[index]; }         set { ((ilist<t>)this)[index] = (t)value; }     }      public bool isreadonly     {         { return false; }     }      public bool isfixedsize     {         { return false; }     }      public void clear()     {         lock (syncroot)         {             updatelists();             underlyinglist.clear();         }     }      public bool contains(t item)     {         lock (syncroot)         {             updatelists();             return underlyinglist.contains(item);         }     }      public void copyto(t[] array, int arrayindex)     {         lock (syncroot)         {             updatelists();             underlyinglist.copyto(array, arrayindex);         }     }      public bool remove(t item)     {         lock (syncroot)         {             updatelists();             return underlyinglist.remove(item);         }     }      public void copyto(array array, int index)     {         lock (syncroot)         {             updatelists();             underlyinglist.copyto((t[])array, index);         }     }      public int count     {                 {             lock (syncroot)             {                 updatelists();                 return underlyinglist.count;             }         }     }      public object syncroot     {         { return syncroot; }     }      public bool issynchronized     {         { return true; }     }      public int indexof(t item)     {         lock (syncroot)         {             updatelists();             return underlyinglist.indexof(item);         }     }      public void insert(int index, t item)     {         lock (syncroot)         {             updatelists();             underlyinglist.insert(index, item);         }     } } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -