What should we name LockingObject here? Also consider not just the name of the variable but how it looks in-code when locking.

I've seen various examples, but seemingly no solid go-to advice:

Plenty of usages of SyncRoot (and variations such as _syncRoot).

Code Sample:lock(SyncRoot), lock(_syncRoot)

This appears to be influenced by VB's equivalent SyncLock statement, the SyncRoot property that exists on some of the ICollection classes and part of some kind of SyncRoot design pattern (which arguably is a bad idea)

Being in a C# context, not sure if I'd want to have a VBish naming. Even worse, in VB naming the variable the same as the keyword. Not sure if this would be a source of confusion or not.

In the VB SyncRoot MSDN page example, it has a simpleMessageList example with a private messagesLock object

I don't think it's a good idea to name the lock against the type you're locking around ("DictionaryLock") as that's an implementation detail that may change. I prefer naming around the concept/object you're locking ("messagesLock" or "commandQueueLock")

Interestingly, I very rarely see this naming convention for locking objects in code samples online or on StackOverflow.

(EDIT) The C# spec under section "8.12 The Lock Statement" has an example of this pattern and names it synchronizationObject

I'm frequently using this locking pattern (in the code sample provided above) throughout my applications so I thought it might make sense to get a more professional opinion/discussion about a solid naming convention for them. Thanks!

I've come upon this habit after having classes where I may have multiple locks for different resources, so I name the locking object based on what resources it's locking access to. Sometimes one object may be locking multiple resources in which case I would try to make the names respect that in some way, so the reader knows "other locks for those individual resources will be in contention with this lock as well".

@svick Totally possible I'm misusing the term, I still think being specific about the resource being locked is important but if it makes more sense, I'll edit this to suggest the word "Lock" in place of "SynchronizationContext".
–
Jimmy HoffaOct 10 '12 at 18:42

I usually call it locker, but since it's private, I believe it's a somewhat unimportant implementation detail. First rule of thumb, use your team's/company's standards. If there is not one, well, please make one and use it, but in making it, keep things simple. If your class has a single locking object, calling it foo is good enough. If you have many, a good order of business might just be revisiting that class's design first to see if it's doing too many different things. But, if not, then the name becomes important, as in this completely contrived example:

I totally agree with a naming style more or less like this for multiple locking mechanisms. I too had something very similar to this style when I implemented a class with two lockers (also refactored it away later)
–
Chris SinclairOct 10 '12 at 18:42

I always used lck_. That way if you ctrl+f 'lck' then you should only find the locks whilst 'lock' will also find things like 'clock'.

Things like lockThis and Padlock are fine for uni portfolios but for proper projects you should really use semantic names so that when you look at the declarations you know what the object actually does without searching through the code.

I'm not keen on short forms of names. On the one hand it makes sense to have a unique(ish) naming style to find all usages. On the other hand, and maybe I've been lucky, I haven't had the need to find locks; I imagine if I would have to searching for "lock(" would give me a good enough result with few enough false-positives to easily ignore.
–
Chris SinclairOct 10 '12 at 18:44