0

I'm logging values in memory to a list type structure, and would like to keep only the items collected within the last hour. This is in an asp.net mvc app, so the list is static and access by multiple threads. I'm currently checking every 80 minutes and will delete only the ones older than 60 minutes (just so it's not performed on every request). I have a lock around the code that walks through the structure to find old values, collects these to a temp holder, then removes them from the original structure.

I seem to be getting some locking issues with this, so was curious if there is some kind of built-in structure or some bulletproof code that would assist me with doing this, instead of trying to hand code it.

Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
  • How about showing us code that your using currently. Also, you might check out [codereview.se] – crthompson Nov 04 '14 at 17:14
  • http://msdn.microsoft.com/en-us/library/dd997373(v=vs.110).aspx - is a good place to start. Out of curiosity, what are you using as the backing store for your data? – Keith Payne Nov 04 '14 at 17:18
  • 2
    It's *very* hard to tell what's wrong with the code without seeing it. It's unclear why you need a temporary holder though... prepending to a linked list would let you prune by just removing items from the tail very easily... – Jon Skeet Nov 04 '14 at 17:23
  • What does "getting some locking issue" mean? Poor performance? Dead locks? Unpredictable behavior due to NOT locking? – Evgeniy Berezovsky Jun 11 '15 at 23:47
  • I actually forgot about this post, if I recall the issue was actually being caused by trying to remove items from a list that I was currently iterating over. Woops ;) I'll add detail if I can find the code again. Thanks – Brady Moritz Jun 13 '15 at 01:42

2 Answers2

1

As you say you "log" to your List, it seems all you need from that list is to thread-safely

  • add items to the end
  • remove items to the front
  • iterate over it
  • performance (I assume "getting some locking issues" means poor performance)

If so, just use a ConcurrentQueue<T>. That removes the need for locking, and its enumerator

represents a moment-in-time snapshot of the contents of the queue. It does not reflect any updates to the collection after GetEnumerator was called. The enumerator is safe to use concurrently with reads from and writes to the queue.

The enumerator returns the collection elements in the order in which they were added, which is FIFO order (first-in, first-out).

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
-1

Why you just don't use thread-safe collections for such behaviour? Trying to discover a byke?

Another approach can be to use Reactive Rx (but the second way could have lower performance, but it's really great in developing).

As long, as LINQ is not thread-safe, and you should use this (what you already probably do):

lock (lockObject)
{
    newCollection = yourCollection.Where(x => ...);
}

you can use observables instead, which are similar in querying, but are thread-safe.

Community
  • 1
  • 1
Agat
  • 4,577
  • 2
  • 34
  • 62