473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

What is difference between lock(private instance) and lock(private static instance)?

I've read some discuession about lock() for thread-safe. I am wondering
what will be the differce between below two code segment?

Code 1:

class A
{
private static Object padlock = new Object();

...// some codes

public static Method()
{
lock(padlock)
{
...// some codes

}
}
}

Code 2:
class A
{

...// some codes

public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes

}
}
}

My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?

Thanks,

Aug 31 '06 #1
8 2595
Yes, you are correct sir!
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ny********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
I've read some discuession about lock() for thread-safe. I am wondering
what will be the differce between below two code segment?

Code 1:

class A
{
private static Object padlock = new Object();

...// some codes

public static Method()
{
lock(padlock)
{
...// some codes

}
}
}

Code 2:
class A
{

...// some codes

public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes

}
}
}

My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?

Thanks,

Aug 31 '06 #2
I assume that in code 2 you meant to place padLock outside of Method.
When synchronizing static methods you usually use a static lock object
and when using instance methods you usually use an instance lock
object. It really depends on what you're accessing inside the lock
block. For example, if you have an instance method that accesses a
static resource then you may want to lock using a static object
instead. But, yes, in general your understanding is correct. Just
don't apply it to every scenario though. Consider what it is that
you're trying to synchronize first.

Brian

ny********@gmail.com wrote:
I've read some discuession about lock() for thread-safe. I am wondering
what will be the differce between below two code segment?

Code 1:

class A
{
private static Object padlock = new Object();

...// some codes

public static Method()
{
lock(padlock)
{
...// some codes

}
}
}

Code 2:
class A
{

...// some codes

public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes

}
}
}

My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?

Thanks,
Aug 31 '06 #3
Thanks for your reply. In fact what we have is trying to access a
public shared Resource from a static method of class. I think we should
use private static variable to lock.
For another case, as you said. "......when using instance methods you
usually use an instance lock object....", so is code example below
correct?

class A
{

private static sharedResource sr;
private Object padlock = new Object();

public void MethodToAccessStaticResource()
{
lock(padlock)
{
...// some codes to access sharedResources..

}
}
}

Thanks,

Brian Gideon wrote:
I assume that in code 2 you meant to place padLock outside of Method.
When synchronizing static methods you usually use a static lock object
and when using instance methods you usually use an instance lock
object. It really depends on what you're accessing inside the lock
block. For example, if you have an instance method that accesses a
static resource then you may want to lock using a static object
instead. But, yes, in general your understanding is correct. Just
don't apply it to every scenario though. Consider what it is that
you're trying to synchronize first.

Brian

ny********@gmail.com wrote:
I've read some discuession about lock() for thread-safe. I am wondering
what will be the differce between below two code segment?

Code 1:

class A
{
private static Object padlock = new Object();

...// some codes

public static Method()
{
lock(padlock)
{
...// some codes

}
}
}

Code 2:
class A
{

...// some codes

public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes

}
}
}

My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?

Thanks,
Aug 31 '06 #4
In general I would say no. The code is probably not thread-safe. The
reason is because you're accessing a static object from within a lock
that uses an instance object. What what might happen is that two or
more separate instances of class A may execute
MethodToAccessStaticResource and all will acquire the lock (since it
was based on an instance object) and all may simultaneously access the
single sharedResource object. Now, if sharedResource itself is
thread-safe then it might be okay, but that's not necessarily the case.
I'm not sure I'm explaining this very well so I apologize.

Brian

nytimes...@gmail.com wrote:
Thanks for your reply. In fact what we have is trying to access a
public shared Resource from a static method of class. I think we should
use private static variable to lock.
For another case, as you said. "......when using instance methods you
usually use an instance lock object....", so is code example below
correct?

class A
{

private static sharedResource sr;
private Object padlock = new Object();

public void MethodToAccessStaticResource()
{
lock(padlock)
{
...// some codes to access sharedResources..

}
}
}

Thanks,

Brian Gideon wrote:
I assume that in code 2 you meant to place padLock outside of Method.
When synchronizing static methods you usually use a static lock object
and when using instance methods you usually use an instance lock
object. It really depends on what you're accessing inside the lock
block. For example, if you have an instance method that accesses a
static resource then you may want to lock using a static object
instead. But, yes, in general your understanding is correct. Just
don't apply it to every scenario though. Consider what it is that
you're trying to synchronize first.

Brian

ny********@gmail.com wrote:
I've read some discuession about lock() for thread-safe. I am wondering
what will be the differce between below two code segment?
>
Code 1:
>
class A
{
private static Object padlock = new Object();
>
...// some codes
>
public static Method()
{
lock(padlock)
{
...// some codes
>
}
}
}
>
Code 2:
class A
{
>
...// some codes
>
public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes
>
}
}
}
>
My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?
>
Thanks,
Aug 31 '06 #5
Hi,
You are correct, padlock should be static too.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
In general I would say no. The code is probably not thread-safe. The
reason is because you're accessing a static object from within a lock
that uses an instance object. What what might happen is that two or
more separate instances of class A may execute
MethodToAccessStaticResource and all will acquire the lock (since it
was based on an instance object) and all may simultaneously access the
single sharedResource object. Now, if sharedResource itself is
thread-safe then it might be okay, but that's not necessarily the case.
I'm not sure I'm explaining this very well so I apologize.

Brian

nytimes...@gmail.com wrote:
>Thanks for your reply. In fact what we have is trying to access a
public shared Resource from a static method of class. I think we should
use private static variable to lock.
For another case, as you said. "......when using instance methods you
usually use an instance lock object....", so is code example below
correct?

class A
{

private static sharedResource sr;
private Object padlock = new Object();

public void MethodToAccessStaticResource()
{
lock(padlock)
{
...// some codes to access sharedResources..

}
}
}

Thanks,

Brian Gideon wrote:
I assume that in code 2 you meant to place padLock outside of Method.
When synchronizing static methods you usually use a static lock object
and when using instance methods you usually use an instance lock
object. It really depends on what you're accessing inside the lock
block. For example, if you have an instance method that accesses a
static resource then you may want to lock using a static object
instead. But, yes, in general your understanding is correct. Just
don't apply it to every scenario though. Consider what it is that
you're trying to synchronize first.

Brian

ny********@gmail.com wrote:
I've read some discuession about lock() for thread-safe. I am
wondering
what will be the differce between below two code segment?

Code 1:

class A
{
private static Object padlock = new Object();

...// some codes

public static Method()
{
lock(padlock)
{
...// some codes

}
}
}

Code 2:
class A
{

...// some codes

public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes

}
}
}

My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since
each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?

Thanks,

Aug 31 '06 #6
Thank you and I think your explanation is wonderful and clear. But in
which case do we create private NON-static instance as lock object and
use it as a parameter of the lock()? I really don't know if such
senario exists.. Or do you have an example to show the case?

Thanks,

Brian Gideon wrote:
In general I would say no. The code is probably not thread-safe. The
reason is because you're accessing a static object from within a lock
that uses an instance object. What what might happen is that two or
more separate instances of class A may execute
MethodToAccessStaticResource and all will acquire the lock (since it
was based on an instance object) and all may simultaneously access the
single sharedResource object. Now, if sharedResource itself is
thread-safe then it might be okay, but that's not necessarily the case.
I'm not sure I'm explaining this very well so I apologize.

Brian

nytimes...@gmail.com wrote:
Thanks for your reply. In fact what we have is trying to access a
public shared Resource from a static method of class. I think we should
use private static variable to lock.
For another case, as you said. "......when using instance methods you
usually use an instance lock object....", so is code example below
correct?

class A
{

private static sharedResource sr;
private Object padlock = new Object();

public void MethodToAccessStaticResource()
{
lock(padlock)
{
...// some codes to access sharedResources..

}
}
}

Thanks,

Brian Gideon wrote:
I assume that in code 2 you meant to place padLock outside of Method.
When synchronizing static methods you usually use a static lock object
and when using instance methods you usually use an instance lock
object. It really depends on what you're accessing inside the lock
block. For example, if you have an instance method that accesses a
static resource then you may want to lock using a static object
instead. But, yes, in general your understanding is correct. Just
don't apply it to every scenario though. Consider what it is that
you're trying to synchronize first.
>
Brian
>
ny********@gmail.com wrote:
I've read some discuession about lock() for thread-safe. I am wondering
what will be the differce between below two code segment?

Code 1:

class A
{
private static Object padlock = new Object();

...// some codes

public static Method()
{
lock(padlock)
{
...// some codes

}
}
}

Code 2:
class A
{

...// some codes

public static Method()
{
private Object padlock = new Object(); //HERE it is NOT static
variable
lock(padlock)
{
...// some codes

}
}
}

My understanding is only Code1 works fine, since it will lock on a
common static variable for all threads. Code2 will NOT work since each
time it will create a private instance, and lock will be on different
(new created) instance each time. Am I correct?

Thanks,
Aug 31 '06 #7
CoolFish wrote:
Thank you and I think your explanation is wonderful and clear. But in
which case do we create private NON-static instance as lock object and
use it as a parameter of the lock()? I really don't know if such
senario exists.. Or do you have an example to show the case?

Thanks,
An instance object may be used for the lock anytime your code is only
accessing instance members.

public class Example
{
private Object lockObject = new Object();
private int foo = 5;

public void DoSomething()
{
lock (lockObject)
{
foo = foo + 5;
}
}
}

Brian

Aug 31 '06 #8
OK I got it this time. ;)
I think one senario could be that we use the instance method as a
parameter passed to the ThreadStart() delegate.. then we will have the
case that instance method accesses instance memeber within different
thread, thus we need lock() on the instance object.

Thanks for all replys ! :)

Brian Gideon wrote:
CoolFish wrote:
Thank you and I think your explanation is wonderful and clear. But in
which case do we create private NON-static instance as lock object and
use it as a parameter of the lock()? I really don't know if such
senario exists.. Or do you have an example to show the case?

Thanks,

An instance object may be used for the lock anytime your code is only
accessing instance members.

public class Example
{
private Object lockObject = new Object();
private int foo = 5;

public void DoSomething()
{
lock (lockObject)
{
foo = foo + 5;
}
}
}

Brian
Sep 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bruce | last post by:
OK, this won't compile saying it can't access private members declared in class F. I don't get it and even if I make the entire class public, it still says that. I realize it has something to do...
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
4
by: memememe | last post by:
in java if I was calling a synchronized block and wanted to sync it even for static blocks of code I could do a synchronize(theObject.getClass()), can I do a lock(theObject.GetType()) on C# and...
5
by: Zürcher See | last post by:
I have a main form, which pop other forms (children forms). The children forms have a delegate of a function, let call it EndFunction, of the main form, that they call when they ends their work....
6
by: ~~~ .NET Ed ~~~ | last post by:
Yes, I think so at least... In C# you *can* have static properties which are quite useful when used properly. Now imagine the scenario where you need the ability (sp?) to implement a variety of...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
5
by: blah, blah, blah | last post by:
I'm developing a .Net web application and created many helper classes often using static (shared in VB.Net) methods. Do I need to use the lock (SyncLock) statement in these methods to prevent...
20
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.