[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Thread and struct, safety considerations...

Lloyd Dupont

10/18/2003 5:02:00 PM

I have multiple thread which do some operation in a class and I have a
DateTime LastOperation;
value which is write without lock like:
void DoSomething()
{
// something
LastOperation = DateTime.Now;
}

I avoid lock, because the value is not critical at all and could be false I
don't care. However I don't want it to be corrupted !

So I wonder if mulitple thread at a time call something like
myStructField = aStructValue;

is it an affectation (which is thread safe) or a copy (which is thread
unsafe) ?
and in the particular case of DateTime which just a long internally (I
suppose) is it safe anyway ?


2 Answers

Michael Giagnocavo

10/18/2003 9:49:00 PM

0

I believe that only 32-bit operations are atomic (writing a long is not).
Thus, if assigning by value a longer struct could get corrupted. This won't
happen with reference types (since only the 32bit reference is copied).

-mike
MVP

"Lloyd Dupont" <net.galador@ld> wrote in message
news:%23oji7lZlDHA.744@tk2msftngp13.phx.gbl...
> I have multiple thread which do some operation in a class and I have a
> DateTime LastOperation;
> value which is write without lock like:
> void DoSomething()
> {
> // something
> LastOperation = DateTime.Now;
> }
>
> I avoid lock, because the value is not critical at all and could be false
I
> don't care. However I don't want it to be corrupted !
>
> So I wonder if mulitple thread at a time call something like
> myStructField = aStructValue;
>
> is it an affectation (which is thread safe) or a copy (which is thread
> unsafe) ?
> and in the particular case of DateTime which just a long internally (I
> suppose) is it safe anyway ?
>
>


Lloyd Dupont

10/18/2003 11:52:00 PM

0

mmhh.. better follow your belief, I believe.... ;-)

"Michael Giagnocavo [MVP]" <mggUNSPAM@Atrevido.net> a écrit dans le message
de news:uvlMmPclDHA.2080@TK2MSFTNGP10.phx.gbl...
> I believe that only 32-bit operations are atomic (writing a long is not).
> Thus, if assigning by value a longer struct could get corrupted. This
won't
> happen with reference types (since only the 32bit reference is copied).
>
> -mike
> MVP
>
> "Lloyd Dupont" <net.galador@ld> wrote in message
> news:%23oji7lZlDHA.744@tk2msftngp13.phx.gbl...
> > I have multiple thread which do some operation in a class and I have a
> > DateTime LastOperation;
> > value which is write without lock like:
> > void DoSomething()
> > {
> > // something
> > LastOperation = DateTime.Now;
> > }
> >
> > I avoid lock, because the value is not critical at all and could be
false
> I
> > don't care. However I don't want it to be corrupted !
> >
> > So I wonder if mulitple thread at a time call something like
> > myStructField = aStructValue;
> >
> > is it an affectation (which is thread safe) or a copy (which is thread
> > unsafe) ?
> > and in the particular case of DateTime which just a long internally (I
> > suppose) is it safe anyway ?
> >
> >
>
>