C#: What happens if I try to lock a locked object? [duplicate] - c#

This question already has answers here:
What does a lock statement do under the hood?
(9 answers)
Closed 5 years ago.
for example--
Object obj = new Object();
then I lock one section with lock(obj)
and I lock in other section also lock(obj)
if one thread is in the first section, does that mean that the other section is also locked?

Locking is done on an object, not on a section or location of the code. If one thread takes the lock in the first location, the second thread can't take it anywhere - neither in the same location and nor in any other location.

Related

Several locks in one thread, why might they be needed? [duplicate]

This question already has answers here:
When would you ever use nested locking?
(3 answers)
Closed 11 months ago.
It says here :
While a lock is held, the thread that holds the lock can again acquire and release the lock.
Question. For what purpose can several consecutive locks be used in one thread? Or does it give nothing, but the article says to clarify that inside one thread the code in the second lock will also be executed because the lock is used in the same thread?
I just want to understand the purpose of this information.
Its called recursive locking. It is useful if you have complex paths that may end up trying to lock a resource twice (like in a recursive function). It saves you have to keep track of whether or not you already have a lock.
It is typically implemented as a counter, after the first lock subsequent locks just increment the counter, unlocks decrement, when the count reaches 0 the mutex is released

Is lock required for single writer situation? [duplicate]

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.

C# lambda expression in the loop [duplicate]

This question already has answers here:
Captured variable in a loop in C#
(10 answers)
Closed 5 years ago.
why the varialbe is 2 in the loop?
and it is the 1st breakpoint the program meet in the loop.
Because by the time your breakpoint was hit in the new thread, the main thread had already looped twice. Remember, they're running on separate threads, so unless you use some kind of synchronization mechanism, you won't be able to predict what occurs when.

Is volatile useful at all in a singlethreaded application in C#? [duplicate]

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.

Why should i pass object to lock keyword while synchronizing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
object c = new object();
lock(c)
{
}
Why should i pass object to lock keyword while synchronizing above code.If i pass also where will it be used.I know it is used for acquiring a lock on that object.but i wanted to know how can we acquire lock on object in depth.How does the thread release the lock on the object we pass in lock keyword.
lock statement is a syntactic sugar of using Monitor approach to thread synchronization. Monitor represents an exclusive lock on some resource, and in .NET a resource is an object.
Why you need to give an object reference to a Monitor? Well, because you want to synchronize the access to the whole object in a multi-threaded environment.
How does the thread release the lock on the object we pass in lock
keyword?
Think about lock as follows:
Monitor.Enter(obj);
try
{
// This would be the code inside the "lock" block
}
finally
{
if(Monitor.IsEntered(obj))
Monitor.Exit(obj); // <-- This is how a Monitor releases the lock
}
you don't need to restrict your locking to an Object object, you could lock a Dictionary object for example, to prevent other threads from adding or deleting from the collection.
or
if you don't want to use the lock keyword, you could use [MethodImpl(MethodImplOptions.Synchronized)]
eg.
[MethodImpl(MethodImplOptions.Synchronized)]
static void mySynchronisedTask()
{
//do things that i want to be synchronised
Console.WriteLine("before sync task");
Thread.Sleep(5000);
Console.WriteLine("after sync task");
}

Categories