473,395 Members | 1,403 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,395 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 1977
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
5
by: CannonFodder | last post by:
hi all, im new to c# and i think i may have coded myself into a bit of a mess. basically i am writing a windows service which audits all the pcs on a network using WMI on a regular basis. The...
1
by: Diffident | last post by:
Guys, I have been cracking my head over this concept in .NET framework. I have read many posts on this topic but not clear about this and hence I am posting it again. If you have designed...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
4
by: | last post by:
I find in the documentation the following Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
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...
10
by: Paul | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
2
by: carlos | last post by:
The first application I wrote using asp.net started off rather small, and as a result, the design of the application took a "Rapid Application Development" type of approach. By this I mean that it...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.