1

You can sometimes reuse the object itself for the lock, but quite often it is advised to use a different object anyway.

Don't we have a lot more typesafety and a lot better intention if there would just be a keyword for lock?

private object _MyLock = new object(); // someone would now be able to reassign breaking everything _MyLock = new object(); lock ( _MyLock ) ... 

VS

private lock _MyLock; // compiler error _MyLock = new object(); lock ( _MyLock ) ... 

Before I get downvotes that you can't guess someones intention: I'm pretty sure the language designers had a good reason and there are more knowledgable coders here that now why. I ask this to understand better programming principles.

6
  • perhaps to allow you to lock on different things and save memory Commented Jun 3, 2018 at 21:44
  • Might be as simple as "Because Java did things that way and C# was trying to attract Java programmers" Commented Jun 3, 2018 at 21:52
  • Much more relevant, make _MyLock readonly. Don't reassign a lock object. Commented Jun 3, 2018 at 22:56
  • And what kind of 'type-safety' are you after? Commented Jun 3, 2018 at 22:57
  • 1
    You need something with identity, that you can use static or by instance, public or private, maybe keep in a collection... System.Object already ticks all those boxes, a 'readability` feature is not even so easy to design. You didn't finish it. Commented Jun 4, 2018 at 5:13

3 Answers 3

1

Note that by using an object as a monitor, you can already do all the things you can do with any other object: Pass references so that multiple classes can share the same monitor, keep them in arrays, keep them inside other data structures.

If you had a special type of declaration for a lockable, you would need special syntax for passing it to a function, storing a reference to it inside another instance, associating it with a type instead of an instance, creating arrays (e.g. for LockMany operation), and so on.

Using the language rules that already exist for objects to handle all these common and not-so-common usages makes the language a whole lot simpler.

Sign up to request clarification or add additional context in comments.

Comments

1

Don't we have a lot more typesafety and a lot better intention if there would just be a keyword for lock?

It's not about type safety at all. It's about thread safety.

Sometimes that means running the same code in a single lock over and over. Perhaps you have a single large array, where some of your operations might need to swap two elements, and you want to make sure things are synchronized during the swap. In this kind of context, even a simple lock keyword by itself, where the object is created for you behind the scenes, might be good enough.

Sometimes you're sharing an object among very different sets of code. Now you need multiple lock sections that coordinate using a common object. In this case, the code you're talking about seems to make sense. Letting the compiler create a lock object for you isn't good enough because the different lock sections would not coordinate, but you also want to make sure the common lock object is fixed, and doesn't change somehow. For example, maybe you're working through an array with multiple threads, and you have different operations that might modify a shared index value that indicates which element is considered current or active. Each of these operations should lock on the same object.

But sometimes you share multiple object instances (often of the same type) among several sets of code. Think producer/consumer pattern, where multiple consumers from different threads need to coordinate access to a shared queue, and the consumers themselves are multi-threaded. In this kind of case, a single common lock object would be okay to retrieve an element from the queue, but a single shared object in different sections of the consumer could become a bottleneck for the application. Instead, you would only want to lock once per active object/consumer. You need the lock section to accept a variable that indicates which object needs protection, without locking your entire data set.

Comments

0

One solution may be defining lock object as readonly.

static readonly object lockObject = new object(); 

In this case compiler prevents renewing and assigning new object to lockobject.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.