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

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

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


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

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

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

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

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

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

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