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

I

intrader

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
 
D

DeveloperX

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

intrader

DeveloperX said:
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.
 
I

intrader

DeveloperX said:
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/threads/waithandles.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
 
D

DeveloperX

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 :/
 
D

DeveloperX

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.
 
I

intrader

DeveloperX said:
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.
Thanks for the info; much learned from it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top