Thread Saftey Question

M

Med

Hi,

Could someone please tell me if I undrestand the concept of "Thread Saftey"
correctly in Multi-threaded web application (asp.net 2) using following
examples:


1. Class A is NOT a thread safe class:

public sealed class A
{
private static int _iCustomerId;

//Property Example
public static int CustomerId
{
set { _iCustomerId = value; }
get { return _iCustomerId; }
}

//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
_iCustomerId = iCustomerId;

DataSet ds = Select * from customer where CustomerId = iCustomerId
from database....
return ds;
}
}

2. Class B IS a thread safe class:

public sealed class B
{
//We do not use class-level private/protected variables here.
//Also not using any properties with class-level private/protected
variables.

//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
DataSet ds = Select * from customer where CustomerId = iCustomerId
from database....
return ds;
}
}

So, A class is "Thread Safe" if:
1. As long as no "class-level private/protected variables" are used.
2.Instantiate a new class instead of using static methods/properties.

Am I understanding it correctly?

Thanks for your time


Kind Regards

Med
 
C

Carl Daniel [VC++ MVP]

Med said:
Hi,

Could someone please tell me if I undrestand the concept of "Thread
Saftey" correctly in Multi-threaded web application (asp.net 2) using
following examples:


1. Class A is NOT a thread safe class:

public sealed class A
{
private static int _iCustomerId;

//Property Example
public static int CustomerId
{
set { _iCustomerId = value; }
get { return _iCustomerId; }
}

//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
_iCustomerId = iCustomerId;

DataSet ds = Select * from customer where CustomerId =
iCustomerId from database....
return ds;
}
}

2. Class B IS a thread safe class:

public sealed class B
{
//We do not use class-level private/protected variables here.
//Also not using any properties with class-level private/protected
variables.

//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
DataSet ds = Select * from customer where CustomerId =
iCustomerId from database....
return ds;
}
}

So, A class is "Thread Safe" if:
1. As long as no "class-level private/protected variables" are used.
2.Instantiate a new class instead of using static methods/properties.

Am I understanding it correctly?

You're on the right path yes. Basically, any code that makes no use of
shared state (e.g. static variables) is thread safe (note that shared state
generally includes things linke windows, files, streams - nearly anything
external to your code must be considered shared state unless it's known to
be thread safe).

Generally, this concept can be extended to include any code that doesn't use
mutable shared state (but might use read-only or const shared state). In
this case, the shared state must be const across all threads - if there's
even a single writer and multiple readers, then the readers are not thread
safe without some form of synchronization.

Any code that does use mutable shared state must be considered not thread
safe unless the developer has taken measures to synchronize access to the
shared state. This could be in the form of Monitor.Enter, Mutexes,
Semphores, or any of many other techniques - the right one to use depends on
the use case.

-cd
 
M

Med

Hi again,

Now I have a doubt about the web user controls. Could you please tell me if
the following is thread safe, as I am not sure loading user controls at
runtime will create a new instance or not.

//asp.net web user control
public partial class Customer: System.Web.UI.UserControl
{

private string _sSQLAction;

public string CustomerSQLAction
{
get { return this._sSQLAction; }
set { this._sSQLAction = value; }
}
}


//No loading above web user control from a WebForm as follow:
public partial class AddNewCustomer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Customer uc = (Customer)LoadControl("~/WebUserControls/Customer
objInfo.ascx");
uc.CustomerSQLAction = "Insert";
}
}


Thanks

Med
 
K

Karthik D V

Hi ,
This is still thread safe. Because class level variable is not
shared.
While loading the user control, every time it will create new
instance..
 
M

Med

many thanks.

Med

Karthik D V said:
Hi ,
This is still thread safe. Because class level variable is not
shared.
While loading the user control, every time it will create new
instance..
 

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