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

Thread Locking In Static Methods - How?

P: n/a
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to
do that.

How should I perform thread locking within static methods like this? Thank
you.
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally
I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to
do that.

How should I perform thread locking within static methods like this?
Thank
you.

Nov 16 '05 #2

P: n/a
If I do it this way it seems like I will be loking the whole class instead
of just this method, is that correct? Thank you.

"ShaneB" <st********@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to do that.

How should I perform thread locking within static methods like this?
Thank
you.


Nov 16 '05 #3

P: n/a
No. What this does is cause a lock of any code location that uses the
same lock definition. If every method in the class had its code
encompassed by the same statement it would cause the whole class to lock
anytime one of the methods was entered, but only if that was the case.

If you want to make sure that only the one method is locked you could
create a private static member variable for the specific method, then
lock against that. For example:

private static string _logTextLock = "This is used to lock.";
public static void LogText(string text)
{
lock (_logTextLock)
{
...
}
}

Hope that helps.

Have A Better One!

John M Deal, MCP
Necessity Software

McFly Racing wrote:
If I do it this way it seems like I will be loking the whole class instead
of just this method, is that correct? Thank you.

"ShaneB" <st********@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods.
Normally
I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able
to
do that.

How should I perform thread locking within static methods like this?
Thank
you.



Nov 16 '05 #4

P: n/a
That is correct and after a bit of research, it looks like it's also a bad
idea:

http://msdn.microsoft.com/library/de...ui06032003.asp

What specifially are you doing in your method?

ShaneB
"McFly Racing" <No****@Microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If I do it this way it seems like I will be loking the whole class instead
of just this method, is that correct? Thank you.

"ShaneB" <st********@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
> Thread Locking In Static Methods
>
>
>
> I have the need for a Log Manger class that has static methods. Normally > I
> would use the lock statement or a Monitor statement both of which take
> a
> reference to (this). In the case of these static methods I am not able to > do that.
>
>
>
> How should I perform thread locking within static methods like this?
> Thank
> you.
>
>



Nov 16 '05 #5

P: n/a
Thanks Shane,

There are about a dozen or so methods in an existing class I am cleaning up.
I will set up a few tests and see what I can come up with. I will post my
solution here when finished. Thanks again.

"ShaneB" <st********@yahoo.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...
That is correct and after a bit of research, it looks like it's also a bad
idea:

http://msdn.microsoft.com/library/de...ui06032003.asp
What specifially are you doing in your method?

ShaneB
"McFly Racing" <No****@Microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If I do it this way it seems like I will be loking the whole class instead of just this method, is that correct? Thank you.

"ShaneB" <st********@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
> Thread Locking In Static Methods
>
>
>
> I have the need for a Log Manger class that has static methods.

Normally
> I
> would use the lock statement or a Monitor statement both of which take > a
> reference to (this). In the case of these static methods I am not
able to
> do that.
>
>
>
> How should I perform thread locking within static methods like this?
> Thank
> you.
>
>



Nov 16 '05 #6

P: n/a
Yes, the lock keyword is a terribly named keyword

http://www.dotnetconsult.co.uk/weblo...d-a68b98b24457

However, I'm not sure about your implementation. Three things:

1. Its a bit heavyweight to allocate a string just for locking
2. Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned
3. If anyone updates the string they will end up with a different object and so you then have a race condition

Its much simpler to use

private static readonly object myGuardObject = new object();

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

No. What this does is cause a lock of any code location that uses the
same lock definition. If every method in the class had its code
encompassed by the same statement it would cause the whole class to lock
anytime one of the methods was entered, but only if that was the case.

If you want to make sure that only the one method is locked you could
create a private static member variable for the specific method, then
lock against that. For example:

private static string _logTextLock = "This is used to lock.";
public static void LogText(string text)
{
lock (_logTextLock)
{
...
}
}

Hope that helps.

Have A Better One!

John M Deal, MCP
Necessity Software

Nov 16 '05 #7

P: n/a
ShaneB <st********@yahoo.com> wrote:
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}


Both lock(this) and lock(typeof(ThisClassType)) are a bad idea, IMO.

See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
Great catch. The funny thing is that I've actually implemented it in
code the way you show it and not the way I did. Some time ago I had read
Jeff Richter's article
(http://msdn.microsoft.com/msdnmag/issues/03/01/NET/) on this topic and
I use it as a reference anytime I need it.

I suppose where I screwed up was by responding from memory rather than
being sure to check the real thing. Thanks for calling me on it though,
it'll help remind me to check my references (and code) before passing on
what could turn out to be detrimental information to others.

Have A Better One!

John M Deal, MCP
Necessity Software

Richard Blewett [DevelopMentor] wrote:
Yes, the lock keyword is a terribly named keyword

http://www.dotnetconsult.co.uk/weblo...d-a68b98b24457

However, I'm not sure about your implementation. Three things:

1. Its a bit heavyweight to allocate a string just for locking
2. Because you use a string literal, anyone else locking the same string literal will be in contention with you as the string is interned
3. If anyone updates the string they will end up with a different object and so you then have a race condition

Its much simpler to use

private static readonly object myGuardObject = new object();

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

No. What this does is cause a lock of any code location that uses the
same lock definition. If every method in the class had its code
encompassed by the same statement it would cause the whole class to lock
anytime one of the methods was entered, but only if that was the case.

If you want to make sure that only the one method is locked you could
create a private static member variable for the specific method, then
lock against that. For example:

private static string _logTextLock = "This is used to lock.";
public static void LogText(string text)
{
lock (_logTextLock)
{
...
}
}

Hope that helps.

Have A Better One!

John M Deal, MCP
Necessity Software

Nov 16 '05 #9

P: n/a
Hi,

What you need is a locking object. In this case, it would be a static member
of the class. Construct the locking object in the static constructor of the
class.

Then, in each static method, lock on the locking object.

The locking object need not be anything special, just an Object instance
will do.

Other people suggested locking on the type of the class, which is not a good
way to do things in any event (Check Skeet's threading pages for more
explanation).

HTH,
TT

"McFly Racing" wrote:
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to
do that.

How should I perform thread locking within static methods like this? Thank
you.

Nov 16 '05 #10

P: n/a
Thanks for the link. Interesting reading.

ShaneB

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
ShaneB <st********@yahoo.com> wrote:
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}


Both lock(this) and lock(typeof(ThisClassType)) are a bad idea, IMO.

See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.