how to lock a method in class
-
Hi all, I want to lock a particular method of the object, so it allows only single access of the method at one time. But by using lock(this) to the method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ? Thanks
-
Hi all, I want to lock a particular method of the object, so it allows only single access of the method at one time. But by using lock(this) to the method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ? Thanks
Create a new meber variable (e.g. object lockVarForMethodA) and reference a unique object of your choice (e.g. new object()). Than use lock(lockVarForMethodA) in your method.
-
Hi all, I want to lock a particular method of the object, so it allows only single access of the method at one time. But by using lock(this) to the method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ? Thanks
ting668 wrote: But by using lock(this) to the method will cause the whole object to be locked, That's not true. The object is not locked in such a way that nothing else can use it. Unless a method also calls
lock(this)
or otherwise locks whatever objectthis
refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method). What Robert said is good, though. If you want to synchronize each method individually then create separate static instances of objects to lock against. These must be static so that they are the same object for all instances of your object in any thread within a single AppDomain. If you're going to synchronize each method individually and don't need instance data, I recommend you make the methods static. The CLR will synchronize static methods allowing only one thread of execution to enter at a time within a given AppDomain. This would be the same as locking against separate objects for each method yourself and makes for a better design and uses less memory (additional objects are not necessary). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
ting668 wrote: But by using lock(this) to the method will cause the whole object to be locked, That's not true. The object is not locked in such a way that nothing else can use it. Unless a method also calls
lock(this)
or otherwise locks whatever objectthis
refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method). What Robert said is good, though. If you want to synchronize each method individually then create separate static instances of objects to lock against. These must be static so that they are the same object for all instances of your object in any thread within a single AppDomain. If you're going to synchronize each method individually and don't need instance data, I recommend you make the methods static. The CLR will synchronize static methods allowing only one thread of execution to enter at a time within a given AppDomain. This would be the same as locking against separate objects for each method yourself and makes for a better design and uses less memory (additional objects are not necessary). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]HI "Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method)." That means if a class like this below:
Class class1 { public void MethodA(){ lock(this) { ... } } public void MethodB() { ... } }
Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? Is lock useless for single thread programming? Thanks -
HI "Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method)." That means if a class like this below:
Class class1 { public void MethodA(){ lock(this) { ... } } public void MethodB() { ... } }
Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? Is lock useless for single thread programming? Thanksting668 wrote: Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? That is correct. Just try it. The majority of code snippets I post here in this forum I simply type in a simple text editor and compile on the command line, then run. Simple. ting668 wrote: Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? A single static method is synchronized by the CLR. If you want to synchronize different static methods you will need to use
lock
or some equivalent. ting668 wrote: Is lock useless for single thread programming? Yes. Within a single thread you can only execute one statement at a time. With that in mind there's no way you could execute the same method (or act on the same context, like getting or setting a serial number) at the same time. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
ting668 wrote: Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? That is correct. Just try it. The majority of code snippets I post here in this forum I simply type in a simple text editor and compile on the command line, then run. Simple. ting668 wrote: Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? A single static method is synchronized by the CLR. If you want to synchronize different static methods you will need to use
lock
or some equivalent. ting668 wrote: Is lock useless for single thread programming? Yes. Within a single thread you can only execute one statement at a time. With that in mind there's no way you could execute the same method (or act on the same context, like getting or setting a serial number) at the same time. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]Hi Health, Class class1 { public void MethodA() { lock (this) { MethodB(); } } public void MethodB() { lock (this) { ... } } } From the code above, statements in locking block of MethodB still successfully run which is branched within the lock from MethodA. Why it is not a counter example of the locking theroy of the point1 : Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? of the last reply? Thanks
-
Hi Health, Class class1 { public void MethodA() { lock (this) { MethodB(); } } public void MethodB() { lock (this) { ... } } } From the code above, statements in locking block of MethodB still successfully run which is branched within the lock from MethodA. Why it is not a counter example of the locking theroy of the point1 : Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? of the last reply? Thanks
Because it's executing within the same thread - not a different thread. Synchronization is not a replacement for state. You really should read Threading[^] in the .NET Framework SDK, as well as about the Monitor.Enter[^] method that the
lock
keyword (SyncLock
in VB.NET) compiles to. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]