I have the following small classes:
//----------------code---------------
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ValidatorsLibra ry
{
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.V alidate(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.V alidate?
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 7 1994
On 5 Feb, 18:43, intrader <intra...@aol.c omwrote:
I have the following small classes:
//----------------code---------------
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ValidatorsLibra ry
{
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.V alidate(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.V alidate?
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 ManualResetEven t 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.
DeveloperX <nn*****@operam ail.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
DeveloperX wrote:
On 5 Feb, 18:43, intrader <intra...@aol.c omwrote:
>I have the following small classes: //----------------code--------------- using System; using System.Collecti ons.Generic; using System.Text;
namespace ValidatorsLibra ry { 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.V alidate(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.V alidate?
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 ManualResetEven t 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 ManualResetEven t from within
FieldValidator. Validate. It seems to me that the caller could simply say
string sErr;
lock(ValidatorB ase._lock){
//add public static Object _lock = new Object() to ValidatorBase
sErr = FieldValidator. Validate(someva lue, 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.
>
DeveloperX wrote:
On 5 Feb, 18:43, intrader <intra...@aol.c omwrote:
>I have the following small classes: //----------------code--------------- using System; using System.Collecti ons.Generic; using System.Text;
namespace ValidatorsLibra ry { 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.V alidate(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.V alidate?
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 ManualResetEven t 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 ManualResetEven t 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 ManualResetEven t.
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(ValidatorB ase._lock){
eStr = FieldValidator. Validate(someva lue,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
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.com wrote:
DeveloperX <nntp...@operam ail.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
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.c omwrote:
DeveloperX wrote:
On 5 Feb, 18:43, intrader <intra...@aol.c omwrote:
I have the following small classes:
//----------------code---------------
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ValidatorsLibra ry
{
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.V alidate(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.V alidate?
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 ManualResetEven t 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 ManualResetEven t from within
FieldValidator. Validate. It seems to me that the caller could simply say
string sErr;
lock(ValidatorB ase._lock){
//add public static Object _lock = new Object() to ValidatorBase
sErr = FieldValidator. Validate(someva lue, 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.
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.c omwrote:
>DeveloperX wrote:
>>On 5 Feb, 18:43, intrader <intra...@aol.c omwrote: I have the following small classes: //----------------code--------------- using System; using System.Collecti ons.Generic; using System.Text; namespace ValidatorsLibra ry { 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.V alidate(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.V alidate? 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 ManualResetEven t 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 ManualResetEven t from within FieldValidator .Validate. It seems to me that the caller could simply say
string sErr; lock(ValidatorB ase._lock){ //add public static Object _lock = new Object() to ValidatorBase sErr = FieldValidator. Validate(someva lue, 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 implementatio n.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
potential problems for thread-safety. So far, I'm only confused.
I need a proper explanation for the concept so I can understand how
to write thread-safe functions in the future.
My apologies for posting a long routine.
|
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 main processes are
performed in a Windows Service which scans Active Directory and for
each computer found it adds a process to the threadpool.
the process creates a new instance of a class which does the WMI
querying, this creates an XML file...
|
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 your class based on singleton pattern where ONLY ONE
instance of class exists for the WHOLE APPLICATION DOMAIN....how can the
public methods in that class be thread-safe? I have read thru posts where
they say that singleton class methods are...
|
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
multithreaded operations. Any instance members are not guaranteed to be
thread safe."
I have 2 questions:
1. I thought dynamic variables are thread-safe since threads have their own
|
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 guaranteed to be
thread safe.
Then, I go to the members documentation and I find public methods or
| |
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 as a single application.
I was wondering if there is a "Thread-Safety Analysis Wizard"?
I'm sure I'm grossly off-base with the following, so I'm prepared to be
embarrassed. Please point me in the right direction!
|
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 Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.'
So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
|
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 thread safety issue.
Are consts thread safe? Would the following example create any thread
safety issues? Would you recommend using static readonly members
instead of constants?
|
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 was
simply built using a simple 3 tier architecture, where the interface
was created using css and master pages, the middle layer or business
logic classes, consisted of some simple public classes that defined
some of the business rules, and the...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |