Tagged Questions
1
vote
2answers
49 views
Correct way to delete elements from a ConcurrentDictionary with a predicate
I have written a caching wrapper class for ConcurrentDictionary. Basically it has a timer that checks if items are expired and removes them. Since ConcurrentDictionary does not have RemoveAll method ...
0
votes
1answer
36 views
Is this code thread safe?
I do a code review for ASP.NET MVC RESTful service application code. Controller looks something like
public class BarController : ApiController
{
[HttpPost]
public void DoBar(byte[] rawData)
...
2
votes
1answer
37 views
Synchronization of remote files download
Preamble: it's a self-assigned and pure syntetic task to learn (and remember what I already knew) C# threads and synchronization and data structures.
The original question was here ...
2
votes
4answers
162 views
Designing a better logger class
Could you please critisize the logger class below? Can it be used in a multi threaded web environment? If not how can I improve it? Is there anything wrong with locking in WriteToLog method or ...
4
votes
3answers
99 views
Is this Agent/Actor implementation issue free?
I implemented this Agent class for a project recently and was wondering if I could get some other eyes to look at it -- I'm currently the only developer where I work so I can't exactly ask someone ...
2
votes
1answer
177 views
Publisher/Consumer thread-safe lock-free queue with a single publisher/consumer
The code above is a implementation of a lock-free queue that makes the assumption that there is exactly one Consumer thread and one Producer thread. This works as intended? The memory barriers is used ...
4
votes
6answers
403 views
Strategy for avoiding threadpool starvation while performing cpu bound jobs in a queued fashion
My aim is to avoid using threadpool threads for CPU bound work, thus avoiding a situation where IIS stops responding to new requests.
Can you see any problems with the code below? Is this a ...
4
votes
1answer
190 views
Is this custom dictionary thread-safe?
I'm writing a custom dictionary which is to be used as a helper for caching. The reason I use delegates is because it must interface with generated code, so I can't change this. However, you can ...
7
votes
2answers
164 views
Synced/Atomic access
Forward
I would love any comments you have, any ideas, any flaws you can find, and any suggestions you might have regarding the code below.
If this is similar to other implementations, I would love ...
8
votes
2answers
862 views
Add/Remove items thread-safely in List<T>
Recently I had to lock collections (of type List<T>) when items were added or removed. Because several collections were used in given code instead of creating helper methods for each collection, ...
5
votes
1answer
86 views
Is this a thread safe way to control access to a reusable resource? Is there a better way?
public static class ThreadStatic<T> where T : new()
{
[ThreadStatic]
private static T instance;
public static T Instance
{
get { return instance ?? (instance = new T()); ...
5
votes
2answers
237 views
How can I make this fast and more readable?
I made a simple library to help me doing A/B tests in a web app. The idea is simple: I have two or more page options (url) for a given page and every call to the library method should give me a url so ...
3
votes
1answer
135 views
C# Array Mutator Methods to mimic JavaScript Array Mutator Methods
Between .NET's built-in collection libraries and LINQ extension methods, I never have to use arrays. An unfortunate consequence of this is that I am probably less competent in working with this very ...
3
votes
1answer
321 views
.Net caching service - thread safety
I have a simple cache service that i am using inside my WPF prism application, i am concerned about the thread safety of it - we will be accessing this via multiple threads and using the current code ...
4
votes
2answers
420 views
thread-safe lock-free counter
I was using such code:
class Counter
{
private int i = 0;
public int Next()
{
lock (this)
{
return i++;
}
}
public void Reset()
{
...
2
votes
0answers
526 views
The correct (and safe) way to use a Timer to poll a Delegate
This is a followup to Polling loop to run in a background thread where I was initially using a single thread and Thread.Sleep() to poll a delegate. I was recommended to use a timer and the ThreadPool ...
1
vote
1answer
1k views
Simple generic multithreaded queue
This is a simple thread safe queue that is used in a producer-consumer model.
public class ThreadedQueue<TType>
{
private Queue<TType> _queue;
private object _queueLock;
...
1
vote
1answer
2k views
Generic wrapper for System.Runtime.Caching.MemoryCache
I have implemented a simple generic wrapper for MemoryCache class. Its interface looks like ConcurrentDictionary class but I tried to keep it simple. I tried to keep operations atomic, hoping not to ...
4
votes
2answers
2k views
Did I need to use lock to ensure that Queue.Dequeue is Thread Safe in this case on .NET 2.0?
Is this ok? I am using C# and .NET 2.0
I have this Queue declared in my class :
static private Queue<SignerDocument> QUEUE = new Queue<SignerDocument>();
I fill this Queue with some ...
4
votes
2answers
843 views
Is this collection actually thread safe? Is concurrent Iterating, querying and modifying safe?
This is based on Alexey Drobyshevsky's excellent article Problems with Iteration
At Alexey's suggestion, I implemented his solution using a ReaderWriterLockSlim. Then I fleshed out a ThreadSafeList : ...
3
votes
3answers
547 views
Is this code OK, to thread safety and delegates?
I've created some code, in main class loop are generated numbers (0~100), and when is generated number > 20 its value is passed to the thread where is simulated some work with this number. Meanwhile ...
3
votes
1answer
522 views
How thread safe is this class?
So the way I understand this code I wrote, it is thread safe, as long as the retrievers and depositors are thread safe. The only bad state I could see occurring is that a thread is using a retriever ...
11
votes
1answer
3k views
Extension methods to make ConcurrentDictionary GetOrAdd and AddOrUpdate thread safe when using valueFactory delegates
The ConcurrentDictionary<T,V> in .NET 4.0 is thread safe but not all methods are atomic. http://msdn.microsoft.com/en-us/library/dd997369.aspx points out that:
... not all methods are ...
6
votes
3answers
985 views
ThreadSafeObservableCollection of (T)
The idea here is to implement a simple, threadsafe, observable collection that clients can bind to, whilst background threads can update. Changes in the contained items raise the CollectionChanged ...
4
votes
1answer
287 views
Can you spot a threading bug in this implementation of costreams?
I am not very good with thread-safety and often fall prey to subtle issues in concurrency. Therefore, I hope that someone here might be able to tell me whether there is a subtle concurrency issue ...