Replies: 2 comments
-
Duplicate of #892? |
Beta Was this translation helpful? Give feedback.
-
You create a You don't have to create a separate lock object. You can just lock on the instance of the class. Or, since you have very coarse lock granularity, the The CLR was designed to support synchronized methods. You can apply it via the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@TheFanatr commented on Wed Dec 13 2017
I am working on a project that, due to speed requirements, needs to be multithreaded; however, the majority of the members in some of the classes I am writing need to be locked with the C# "lock(object x)" statement so that the value of something isn't changed in the middle of a method execution on another thread. This means that I need to create a static lock object for every class that needs it, and add lock(lock) { /* code */ } to every member that needs to be locked. This just clutters everything, and doesn't need to. Multithreading is used a lot, and most of the times requires that members be thread-safe; there should be an easier and/or less wasteful way to make this possible. My proposal is that the following syntax should be allowed to achieve the same functionality:
I realize that there may be consistency or logic issues with the proposal, but I as well as probably many others would really appreciate some kind of system to do this. It would save a lot of time and clutter. Also, note that the provided code sample isn't real code, I just came up with it to show what I mean.
@jcouv commented on Wed Dec 13 2017
Moving language design discussions to csharplang. Thanks
Beta Was this translation helpful? Give feedback.
All reactions