473,396 Members | 1,859 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,396 software developers and data experts.

Thread Locking In Static Methods - How?

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
10 11842
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
2
by: Claes R | last post by:
Hi ! I would like this class method to be thread safe. public static ArrayList List(DateTime date) { ArrayList l = new ArrayList(); l.Add(new ListItemInfo( 1, "S1010")); ...... return l;
7
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs...
2
by: Robert May | last post by:
I have a windows service that spawns multiple threads running a thing that does a bunch of processing. Here's the code that spawns the threads: ...
4
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
1
by: William Sullivan | last post by:
I'm trying to nail down some issues with the cache in my application. Currently, I have an object that stands between my business logic and database logic called CacheLogic (cute, no?). ...
7
by: intrader | last post by:
I have the following small classes: //----------------code--------------- using System; using System.Collections.Generic; using System.Text; namespace ValidatorsLibrary { public class...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
6
by: Anders J | last post by:
Hi We have some code that runs in a EventReceiver ItemAdded handler. The code must be thread-safe since it is iterating a List to find the max number of a column and assigns it + 1 to the Item...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
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...

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.