This question already has answers here:
is locking necessary for Dictionary lookup?
(9 answers)
Closed 5 years ago.
I'm using .netcore with C#, I have a System.Collections.Generic.Dictionary<> that is shared and been access by multiple tasks, but there is only one task that is writing to and updating the key value pairs, and all other tasks(more than one) are only reading from the dictionary. In this situation, do I need to use a lock to protect it, in terms of thread safety?
Yes. Dictionary cannot be safely read from while a write is in process; the internal data storage is not in a valid state, and so exceptions or nonsensical results can arise.
Use a ConcurrentDictionary instead.
Related
This question already has answers here:
When should the volatile keyword be used in C#?
(11 answers)
Closed 7 years ago.
Is there any case in which volatile is useful in the context of single-threaded programming? I know it's used to make sure the value of the variable is always actually checked in memory so is there any case in which that value may change (in a ST app) in a way that the app/compiler won't notice?
No, it's not necessary. It is used to synchronize the memory content between the threads which in case you have only one it doesn't make sense.
This question already has answers here:
Why is lock(this) {...} bad?
(18 answers)
Closed 9 years ago.
There are many posts, votes and answers indicating using lock (this) is not a recommends pattern (not to mention a bad one).
Have a look at this one, for example.
As I'm trying to investigate this pattern a little bit, and wanted to ask whether anyone someone can think of a scenario in which using lock (this) is actually recommended, or even a must?
Locking on THIS is evil. This means that someone may decide to lock on your instance. This means that your instance will wait until someone else releases it.
Rule of thumb: never lock on this but create a seperate (private) object to lock.
But... The problem is deeper: locking has a purpose, by locking you provide protection on the upper object(s) but it doesn't prevent updating the underlying objects in for instance a collection.
In most cases a lock isn't a need. Read up on the subject is what I suggest.
Multiple questions on SO cover you question. Shouldn't be hard to build an opinion about the motivation to not lock on this.
An example and pointers for further reading can be found on the blog of Phil Haack
This question already has answers here:
SortedList<>, SortedDictionary<> and Dictionary<>
(6 answers)
Closed 9 years ago.
I have a large collection of small objects, each has a unique string ident. I need to decide which class to use.
MSDN says about the first two
The two classes have similar object
models, and both have O(log n)
retrieval. Where the two classes
differ is in memory use and speed of
insertion and removal
Since I rarely insert, mostly just retrieve it seems both are good for me. What about the plain old Dictionary?
Plain-old dictionary is the best option if you're not interested in sorting (since it's O(1) retrieval). If you're not going to modify the list much you should use SortedList since it uses less memory.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the correct way to create a single instance application?
What is a good pattern for using a Global Mutex in C#?
Suppose i have created an exe i want that exe must run only once ..how it is possible please give suggestion
If I understand your problem correctly this has nothing to do with having a singleton implementation. You simply need to check if your executable is currently running.
You can do this by calling Process.GetProcesses() or Process.GetProcessesByName(NameOfExecutable) and checking the return values.
Alternatively use a Mutex as suggested above by others.
This question already has answers here:
What's the best way of implementing a thread-safe Dictionary?
(8 answers)
Closed 5 years ago.
whats is the easiest way to make C# dictionary access thread safe? Preferably just using lock(object) but any other ideas welcome!
In .NET 4 you have the ConcurrentDictionary class.
If you need to use an older version of .NET, and want to write it yourself:
wrap a Dictionary as a private field in your class
use a separate private object lockObject
take a lock on that lockObject around every access to the dictionary