473,385 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

2 threading questions

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.
Jun 19 '06 #1
6 1318
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
Jun 19 '06 #2
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" <no***@nowhere.com> wrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
|- 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.
|
|
Jun 19 '06 #3

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>

Jun 19 '06 #4
djc
thanks. Great article.

"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

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>

Jun 19 '06 #5
djc
thanks for the input. I appreciate it.

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:u9**************@TK2MSFTNGP05.phx.gbl...
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" <no***@nowhere.com> wrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
|- 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.
|
|

Jun 19 '06 #6
djc
thanks for the input. I appreciate it.
"Marc Gravell" <ma**********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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

Jun 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
13
by: Varun | last post by:
Hi Friends, Department of Information Technology, Madras Institute of Technology, Anna University, India is conducting a technical symposium, Samhita. As a part of samhita, an Online Programming...
4
by: Antal Rutz | last post by:
Hi, All! I'm new to threading. I have some design questions: Task: I collect data and store them in an RDBMS (mysql or pgsql) The question is how to do that with threading? The...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
4
by: Bob | last post by:
- For cleanup, is it sufficient to set a Thread to Nothing after it's done? - It is OK to pass objects out of the thread? (dumb question maybe but I want to be sure) - What's the best way to...
9
by: akrapus | last post by:
Hi, I am trying to understand how to use threading in Python. I get threading as a concept, but not the implementation. In order to start threading, do you call it as a separate function,...
4
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In...
7
by: kimiraikkonen | last post by:
Hello experts, I've been already working on a project and also asked and i've managed to create a basic Gmail mail sender, but i want to add a progressbar that shows "sending is in progress" but...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.