PC Review


Reply
Thread Tools Rate Thread

2 threading questions

 
 
djc
Guest
Posts: n/a
 
      19th Jun 2006
- kind of new to c# and very new to multithreading -

1) are all objects (value and reference type variables and objects/classes)
safe to *read* from multiple threads? I know if something is not marked as
thread safe you have to use one of the synchronization features to
coordinate *writes* to it but what about simple reads of some global
variables that are accessible to many threads?

2) can I queue up several threads to run a *static* method (each thread
running it with different parameters) without any special synchronization
code? or do I need to make the method not static so a seperate copy of it's
class is instantiated each time?

confused.


 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      19th Jun 2006
1: it isn't clear (from your post) if the values ever change; if they are
set *once* (in the instance/static ctor for the object/type as appropriate)
then you should be fine, and I would mark the backing field as readonly to
indicate this behaviour; if they are updated during execution then no: this
is not reliably thread-safe

2: if the method does not update local state (static fields etc), then you
possibly don't need to do anything special. In particular: if you would
literally be making the method non-static (and not changing any other fields
etc on the class), then this won't achieve anything anyway. Are you worried
about the variables declared inside the method? If so, note that these
typically exist on the calling thread's stack, not against the class, so
each caller (even with 50 at once) has their own copy of these. If you are
worried about static fields that are updated by the method, then yes: this
needs protecting. This would normally be done using a lock on a private,
readonly object (static as required); note that there is also an attribute
you can decorate the required members with, but I can't remember its name
(since I always use lock for more granular control).

Marc


 
Reply With Quote
 
William Stacey [MVP]
Guest
Posts: n/a
 
      19th Jun 2006
Look at from the variable perspective, not so much from the instance/static
method perspective.
1) Write down each instance and static var and mark it (on paper or in a
comment next to the var) as RO (read-only) or RW (readwrite). If a var is
set in the constructor, and will only be read by any threads, then it is RO.
If a var will be set/written outside a constructor, then the var is RW. Any
var that is RO should probably have the "readonly" attribute applied. Local
method vars are created and used by 1 thread, so you don't need to worry
about those.

2) Look at list and apply synchronization to all RW vars.

--
William Stacey [MVP]

"djc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
|- kind of new to c# and very new to multithreading -
|
| 1) are all objects (value and reference type variables and
objects/classes)
| safe to *read* from multiple threads? I know if something is not marked as
| thread safe you have to use one of the synchronization features to
| coordinate *writes* to it but what about simple reads of some global
| variables that are accessible to many threads?
|
| 2) can I queue up several threads to run a *static* method (each thread
| running it with different parameters) without any special synchronization
| code? or do I need to make the method not static so a seperate copy of
it's
| class is instantiated each time?
|
| confused.
|
|


 
Reply With Quote
 
Brian Gideon
Guest
Posts: n/a
 
      19th Jun 2006

djc wrote:
> - kind of new to c# and very new to multithreading -
>
> 1) are all objects (value and reference type variables and objects/classes)
> safe to *read* from multiple threads? I know if something is not marked as
> thread safe you have to use one of the synchronization features to
> coordinate *writes* to it but what about simple reads of some global
> variables that are accessible to many threads?


Technically, no. Even a simple read is not guarenteed to be
thread-safe. The subject is quite confusing. The following article
may help to explain what's going on.

<http://www.yoda.arachsys.com/csharp/threads/volatility.shtml>

 
Reply With Quote
 
djc
Guest
Posts: n/a
 
      19th Jun 2006
thanks. Great article.

"Brian Gideon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> djc wrote:
>> - kind of new to c# and very new to multithreading -
>>
>> 1) are all objects (value and reference type variables and
>> objects/classes)
>> safe to *read* from multiple threads? I know if something is not marked
>> as
>> thread safe you have to use one of the synchronization features to
>> coordinate *writes* to it but what about simple reads of some global
>> variables that are accessible to many threads?

>
> Technically, no. Even a simple read is not guarenteed to be
> thread-safe. The subject is quite confusing. The following article
> may help to explain what's going on.
>
> <http://www.yoda.arachsys.com/csharp/threads/volatility.shtml>
>



 
Reply With Quote
 
djc
Guest
Posts: n/a
 
      19th Jun 2006
thanks for the input. I appreciate it.

"William Stacey [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Look at from the variable perspective, not so much from the
> instance/static
> method perspective.
> 1) Write down each instance and static var and mark it (on paper or in a
> comment next to the var) as RO (read-only) or RW (readwrite). If a var is
> set in the constructor, and will only be read by any threads, then it is
> RO.
> If a var will be set/written outside a constructor, then the var is RW.
> Any
> var that is RO should probably have the "readonly" attribute applied.
> Local
> method vars are created and used by 1 thread, so you don't need to worry
> about those.
>
> 2) Look at list and apply synchronization to all RW vars.
>
> --
> William Stacey [MVP]
>
> "djc" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> |- kind of new to c# and very new to multithreading -
> |
> | 1) are all objects (value and reference type variables and
> objects/classes)
> | safe to *read* from multiple threads? I know if something is not marked
> as
> | thread safe you have to use one of the synchronization features to
> | coordinate *writes* to it but what about simple reads of some global
> | variables that are accessible to many threads?
> |
> | 2) can I queue up several threads to run a *static* method (each thread
> | running it with different parameters) without any special
> synchronization
> | code? or do I need to make the method not static so a seperate copy of
> it's
> | class is instantiated each time?
> |
> | confused.
> |
> |
>
>



 
Reply With Quote
 
djc
Guest
Posts: n/a
 
      19th Jun 2006
thanks for the input. I appreciate it.


"Marc Gravell" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 1: it isn't clear (from your post) if the values ever change; if they are
> set *once* (in the instance/static ctor for the object/type as
> appropriate) then you should be fine, and I would mark the backing field
> as readonly to indicate this behaviour; if they are updated during
> execution then no: this is not reliably thread-safe
>
> 2: if the method does not update local state (static fields etc), then you
> possibly don't need to do anything special. In particular: if you would
> literally be making the method non-static (and not changing any other
> fields etc on the class), then this won't achieve anything anyway. Are you
> worried about the variables declared inside the method? If so, note that
> these typically exist on the calling thread's stack, not against the
> class, so each caller (even with 50 at once) has their own copy of these.
> If you are worried about static fields that are updated by the method,
> then yes: this needs protecting. This would normally be done using a lock
> on a private, readonly object (static as required); note that there is
> also an attribute you can decorate the required members with, but I can't
> remember its name (since I always use lock for more granular control).
>
> Marc
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
threading questions ChunkyMonkey Microsoft VB .NET 6 16th Aug 2009 05:31 AM
.NEt 2.0 : Threading questions Steve B. Microsoft Dot NET Framework 4 9th Feb 2007 02:19 PM
ASP.NET Threading Questions =?Utf-8?B?Sk1heA==?= Microsoft C# .NET 3 15th Mar 2006 06:36 PM
Threading questions **Developer** Microsoft VB .NET 13 3rd Feb 2006 04:29 PM
ASP.NET threading questions hahaha Microsoft ASP .NET 1 12th Nov 2005 02:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 PM.