473,785 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2628
Yes, you are correct sir!
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<ny********@gma il.comwrote in message
news:11******** *************@i 42g2000cwa.goog legroups.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********@gmai l.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 MethodToAccessS taticResource()
{
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********@gmai l.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
MethodToAccessS taticResource 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...@gmai l.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 MethodToAccessS taticResource()
{
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********@gmai l.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*********@ya hoo.comwrote in message
news:11******** **************@ m79g2000cwm.goo glegroups.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
MethodToAccessS taticResource 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...@gmai l.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 MethodToAccessS taticResource()
{
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********@gmai l.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
MethodToAccessS taticResource 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...@gmai l.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 MethodToAccessS taticResource()
{
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********@gmai l.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
1832
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 with the constructors but not what. Second, I'd like to take the line: // Create an vector of 1000 F objects init'd to 1,1 vector<F> F(1000,F(1,1));
7
703
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 keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a
4
10694
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 will it lock for static blocks of code that use that object type?
5
1867
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. By closing the main form, it force the children forms to close too, and before to close himself wait until the last children form has closed. When the children forms are force to close, they will call so rush the EndFunction that I have a...
6
5897
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 classes that must implement an interface. All these classes must have a particular *static* property, and this in particular is handy not only to be consequent with the fact that you can do it with classes but also with the fact that you cannot...
25
5181
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 wanna use global.asax) --- Would you declare your static var as --- public static int x ;
5
1961
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 multiple web application threads from using the same method at the same time? For example, I have a static method that extracts information from a page request cookie for use by other parts of the application. Since this happens on every request I...
20
1954
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 way to define the property below. Thanks Kurt Class1 { private Timer _timer = new Timer(); // Example 1
4
4531
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 child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.