I was working with lock statements and started reading a little more about it. I know the basics on how it works and when to use it, but I tried to understand a little under the hood of it's work and came with some questions I need some help to understand.
1 - Microsoft docs say we should use a reference type. Can we use any reference type? I usually only see people using a new object()
for locks but would it make any practical difference of using for example new List<int>()
as the lock object? I understand it might be a bad practice but would it cause any difference in the app behavior?
2 - What does the lock do to the object? When we use an object to be locked, does the lock operation ever change the object binary value? Does it change the pointer value? Since is a reference type, does the lock use the memory pointer for something?
I found this answer on StackOverflow but it is not exactly what I was searching for, the question is similar but the answers tells more that the lock statement is same as using Monitor class and main behavior, but then what does the Monitor do to the lock object?
I'd like to understand what happens to the program, what is the processor doing when acquiring a lock also what does change in the program memory.