471,594 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Puzzled with Thread Safety of classes with static methods when usedin a Web Application

I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ValidatorsLibrary
{
public class ValidatorBase
{
protected static long _mask;

public ValidatorBase() { }

public static string Validate(string value, long mask)
{
_mask = mask;

//perform common validations here and return "" if OK or
// an error message.

return ""; //when Validate found no problems
}
}
public class FieldValidator : ValidatorBase
{
private FieldValidator() { } //no instantiation of this class

public static string Validate(string value, long mask)
{
string sErr = ValidatorBase.Validate(value, mask);
if (sErr.Length 0) return sErr;
else
{
//do more validations specifice to this class
//these validations use local parameter mask
}
return sErr;
}
}
}
----------------------end of code-----------------------

Questions:

1. I gather that the class variable _mask is not thread safe. Is this so?

2. When FieldValidator.Validate is called, can other threads provide
values for its parameters while being passed to ValidatorBase.Validate?

3. How do I test for Thread Safety?

4. I am thinking of not using static methods and converting these
classes. Is a Singleton pattern appropriate?

Thanks
Feb 5 '07 #1
7 1863
On 5 Feb, 18:43, intrader <intra...@aol.comwrote:
I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ValidatorsLibrary
{
public class ValidatorBase
{
protected static long _mask;

public ValidatorBase() { }

public static string Validate(string value, long mask)
{
_mask = mask;

//perform common validations here and return "" if OK or
// an error message.

return ""; //when Validate found no problems
}
}
public class FieldValidator : ValidatorBase
{
private FieldValidator() { } //no instantiation of this class

public static string Validate(string value, long mask)
{
string sErr = ValidatorBase.Validate(value, mask);
if (sErr.Length 0) return sErr;
else
{
//do more validations specifice to this class
//these validations use local parameter mask
}
return sErr;
}
}}

----------------------end of code-----------------------

Questions:

1. I gather that the class variable _mask is not thread safe. Is this so?

2. When FieldValidator.Validate is called, can other threads provide
values for its parameters while being passed to ValidatorBase.Validate?

3. How do I test for Thread Safety?

4. I am thinking of not using static methods and converting these
classes. Is a Singleton pattern appropriate?

Thanks
1) Quite correct, it may be modified by any thread and therefore you
can't guarentee it will still be the _mask you looked at previously.
2) I keep meaning to write a bit of code to show this as part of my
threading tutorial, I'll do one tomorrow, but if you're eager, set up
a ManualResetEvent to hold the code in a function and then modify a
local variable from a second thread before releasing the MRE.
3) Thread safe simply means if two threads access the same code at the
same time the results will be consistent and accurate for each calling
thread. That's what your test needs to prove. You'll need to know
about locking strategies, synchronous/asynchronous invoking of methods
and firing delegates/events.
4) The singleton pattern kinda relies on a static property to obtain
the singleton, so not really.
See http://www.dofactory.com/Patterns/PatternSingleton.aspx and do
check the bottom link which is their premium singleton
implementation.
Feb 5 '07 #2
DeveloperX <nn*****@operamail.comwrote:

<snip>
4) The singleton pattern kinda relies on a static property to obtain
the singleton, so not really.
See http://www.dofactory.com/Patterns/PatternSingleton.aspx and do
check the bottom link which is their premium singleton
implementation.
Yes - although unfortunately the example they give isn't quite thread-
safe, because the Server property can be accessed from multiple threads
and Random isn't thread-safe :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 5 '07 #3
DeveloperX wrote:
On 5 Feb, 18:43, intrader <intra...@aol.comwrote:
>I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ValidatorsLibrary
{
public class ValidatorBase
{
protected static long _mask;

public ValidatorBase() { }

public static string Validate(string value, long mask)
{
_mask = mask;

//perform common validations here and return "" if OK or
// an error message.

return ""; //when Validate found no problems
}
}
public class FieldValidator : ValidatorBase
{
private FieldValidator() { } //no instantiation of this class

public static string Validate(string value, long mask)
{
string sErr = ValidatorBase.Validate(value, mask);
if (sErr.Length 0) return sErr;
else
{
//do more validations specifice to this class
//these validations use local parameter mask
}
return sErr;
}
}}

----------------------end of code-----------------------

Questions:

1. I gather that the class variable _mask is not thread safe. Is this so?

2. When FieldValidator.Validate is called, can other threads provide
values for its parameters while being passed to ValidatorBase.Validate?

3. How do I test for Thread Safety?

4. I am thinking of not using static methods and converting these
classes. Is a Singleton pattern appropriate?

Thanks

1) Quite correct, it may be modified by any thread and therefore you
can't guarentee it will still be the _mask you looked at previously.
Yes
2) I keep meaning to write a bit of code to show this as part of my
threading tutorial, I'll do one tomorrow, but if you're eager, set up
a ManualResetEvent to hold the code in a function and then modify a
local variable from a second thread before releasing the MRE.
Here I have difficulty with using ManualResetEvent from within
FieldValidator.Validate. It seems to me that the caller could simply say

string sErr;
lock(ValidatorBase._lock){
//add public static Object _lock = new Object() to ValidatorBase
sErr = FieldValidator.Validate(somevalue, somemask);
}

would this work?
3) Thread safe simply means if two threads access the same code at the
same time the results will be consistent and accurate for each calling
thread. That's what your test needs to prove. You'll need to know
about locking strategies, synchronous/asynchronous invoking of methods
and firing delegates/events.
This is subject vast enough for a dissertation!
4) The singleton pattern kinda relies on a static property to obtain
the singleton, so not really.
See http://www.dofactory.com/Patterns/PatternSingleton.aspx and do
check the bottom link which is their premium singleton
implementation.
Yes, thanks. I will avoid this.
The article is quite interesting; I like the lazy evaluation of the
static instance.
>
Feb 5 '07 #4
DeveloperX wrote:
On 5 Feb, 18:43, intrader <intra...@aol.comwrote:
>I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;

namespace ValidatorsLibrary
{
public class ValidatorBase
{
protected static long _mask;

public ValidatorBase() { }

public static string Validate(string value, long mask)
{
_mask = mask;

//perform common validations here and return "" if OK or
// an error message.

return ""; //when Validate found no problems
}
}
public class FieldValidator : ValidatorBase
{
private FieldValidator() { } //no instantiation of this class

public static string Validate(string value, long mask)
{
string sErr = ValidatorBase.Validate(value, mask);
if (sErr.Length 0) return sErr;
else
{
//do more validations specifice to this class
//these validations use local parameter mask
}
return sErr;
}
}}

----------------------end of code-----------------------

Questions:

1. I gather that the class variable _mask is not thread safe. Is this so?

2. When FieldValidator.Validate is called, can other threads provide
values for its parameters while being passed to ValidatorBase.Validate?

3. How do I test for Thread Safety?

4. I am thinking of not using static methods and converting these
classes. Is a Singleton pattern appropriate?

Thanks

1) Quite correct, it may be modified by any thread and therefore you
can't guarentee it will still be the _mask you looked at previously.
2) I keep meaning to write a bit of code to show this as part of my
threading tutorial, I'll do one tomorrow, but if you're eager, set up
a ManualResetEvent to hold the code in a function and then modify a
local variable from a second thread before releasing the MRE.
3) Thread safe simply means if two threads access the same code at the
same time the results will be consistent and accurate for each calling
thread. That's what your test needs to prove. You'll need to know
about locking strategies, synchronous/asynchronous invoking of methods
and firing delegates/events.
4) The singleton pattern kinda relies on a static property to obtain
the singleton, so not really.
See http://www.dofactory.com/Patterns/PatternSingleton.aspx and do
check the bottom link which is their premium singleton
implementation.

I am not sure that this message just duplicates my answer to DeveloperX.

1) Confirms my view.
2) I find the documentation of ManualResetEvent daunting; I found a good
example at
http://www.yoda.arachsys.com/csharp/...handles.shtml; I also
had trouble withe the parametere value,and mask (as to their thread
safety) before using ManualResetEvent.

I then thought of a different solution:

Declare a lock
public static Object _lock = new Object();

In the FieldValidator caller do the following:

string eStr;
lock(ValidatorBase._lock){
eStr = FieldValidator.Validate(somevalue,somemask);
}
I think this should do it. Proving it is another matter. What do you think?
3. Thanks for the info. It is really worth a dissertation.
4. I found the article about Singletons interesting - that implmentation
seems to not be thread safe according to John Skeet.

Thanks
Feb 5 '07 #5
Eep, I've been using that as a good example for ages! I don't use it
in the way they do, so the Random thing has never come up, I had no
idea :/

On 5 Feb, 22:02, Jon Skeet [C# MVP] <s...@pobox.comwrote:
DeveloperX <nntp...@operamail.comwrote:

<snip>
4) The singleton pattern kinda relies on a static property to obtain
the singleton, so not really.
See http://www.dofactory.com/Patterns/Pa...gleton.aspxand do
check the bottom link which is their premium singleton
implementation.

Yes - although unfortunately the example they give isn't quite thread-
safe, because the Server property can be accessed from multiple threads
and Random isn't thread-safe :(

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

Feb 5 '07 #6
I can't see a reason not to, i'll check tomorrow. I use the MRE simply
because I'm mainly 1.1 and it fits well with the production code I
write.
On 5 Feb, 22:35, intrader <intra...@aol.comwrote:
DeveloperX wrote:
On 5 Feb, 18:43, intrader <intra...@aol.comwrote:
I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;
namespace ValidatorsLibrary
{
public class ValidatorBase
{
protected static long _mask;
public ValidatorBase() { }
public static string Validate(string value, long mask)
{
_mask = mask;
//perform common validations here and return "" if OK or
// an error message.
return ""; //when Validate found no problems
}
}
public class FieldValidator : ValidatorBase
{
private FieldValidator() { } //no instantiation of this class
public static string Validate(string value, long mask)
{
string sErr = ValidatorBase.Validate(value, mask);
if (sErr.Length 0) return sErr;
else
{
//do more validations specifice to this class
//these validations use local parameter mask
}
return sErr;
}
}}
----------------------end of code-----------------------
Questions:
1. I gather that the class variable _mask is not thread safe. Is this so?
2. When FieldValidator.Validate is called, can other threads provide
values for its parameters while being passed to ValidatorBase.Validate?
3. How do I test for Thread Safety?
4. I am thinking of not using static methods and converting these
classes. Is a Singleton pattern appropriate?
Thanks
1) Quite correct, it may be modified by any thread and therefore you
can't guarentee it will still be the _mask you looked at previously.
Yes
2) I keep meaning to write a bit of code to show this as part of my
threading tutorial, I'll do one tomorrow, but if you're eager, set up
a ManualResetEvent to hold the code in a function and then modify a
local variable from a second thread before releasing the MRE.

Here I have difficulty with using ManualResetEvent from within
FieldValidator.Validate. It seems to me that the caller could simply say

string sErr;
lock(ValidatorBase._lock){
//add public static Object _lock = new Object() to ValidatorBase
sErr = FieldValidator.Validate(somevalue, somemask);
}

would this work?3) Thread safe simply means if two threads access the same code at the
same time the results will be consistent and accurate for each calling
thread. That's what your test needs to prove. You'll need to know
about locking strategies, synchronous/asynchronous invoking of methods
and firing delegates/events.

This is subject vast enough for a dissertation!4) The singleton pattern kinda relies on a static property to obtain
the singleton, so not really.
See http://www.dofactory.com/Patterns/Pa...gleton.aspxand do
check the bottom link which is their premium singleton
implementation.

Yes, thanks. I will avoid this.
The article is quite interesting; I like the lazy evaluation of the
static instance.


Feb 5 '07 #7
DeveloperX wrote:
I can't see a reason not to, i'll check tomorrow. I use the MRE simply
because I'm mainly 1.1 and it fits well with the production code I
write.
On 5 Feb, 22:35, intrader <intra...@aol.comwrote:
>DeveloperX wrote:
>>On 5 Feb, 18:43, intrader <intra...@aol.comwrote:
I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;
namespace ValidatorsLibrary
{
public class ValidatorBase
{
protected static long _mask;
public ValidatorBase() { }
public static string Validate(string value, long mask)
{
_mask = mask;
//perform common validations here and return "" if OK or
// an error message.
return ""; //when Validate found no problems
}
}
public class FieldValidator : ValidatorBase
{
private FieldValidator() { } //no instantiation of this class
public static string Validate(string value, long mask)
{
string sErr = ValidatorBase.Validate(value, mask);
if (sErr.Length 0) return sErr;
else
{
//do more validations specifice to this class
//these validations use local parameter mask
}
return sErr;
}
}}
----------------------end of code-----------------------
Questions:
1. I gather that the class variable _mask is not thread safe. Is this so?
2. When FieldValidator.Validate is called, can other threads provide
values for its parameters while being passed to ValidatorBase.Validate?
3. How do I test for Thread Safety?
4. I am thinking of not using static methods and converting these
classes. Is a Singleton pattern appropriate?
Thanks
1) Quite correct, it may be modified by any thread and therefore you
can't guarentee it will still be the _mask you looked at previously.
Yes
>>2) I keep meaning to write a bit of code to show this as part of my
threading tutorial, I'll do one tomorrow, but if you're eager, set up
a ManualResetEvent to hold the code in a function and then modify a
local variable from a second thread before releasing the MRE.
Here I have difficulty with using ManualResetEvent from within
FieldValidator.Validate. It seems to me that the caller could simply say

string sErr;
lock(ValidatorBase._lock){
//add public static Object _lock = new Object() to ValidatorBase
sErr = FieldValidator.Validate(somevalue, somemask);
}

would this work?3) Thread safe simply means if two threads access the same code at the
>>same time the results will be consistent and accurate for each calling
thread. That's what your test needs to prove. You'll need to know
about locking strategies, synchronous/asynchronous invoking of methods
and firing delegates/events.
This is subject vast enough for a dissertation!4) The singleton pattern kinda relies on a static property to obtain
>>the singleton, so not really.
See http://www.dofactory.com/Patterns/Pa...gleton.aspxand do
check the bottom link which is their premium singleton
implementation.
Yes, thanks. I will avoid this.
The article is quite interesting; I like the lazy evaluation of the
static instance.


Thanks for the info; much learned from it
Feb 6 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jonathan Burd | last post: by
5 posts views Thread by CannonFodder | last post: by
1 post views Thread by Diffident | last post: by
4 posts views Thread by | last post: by
4 posts views Thread by Warren Sirota | last post: by
10 posts views Thread by Paul | last post: by
2 posts views Thread by carlos | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

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.