By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.