2 threading questions

D

djc

- 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.
 
M

Marc Gravell

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
 
W

William Stacey [MVP]

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]

|- 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.
|
|
 
B

Brian Gideon

djc said:
- 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>
 

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