Re: static classes & threading
- From: Frank Rizzo <none@xxxxxxxx>
- Date: Tue, 19 Apr 2005 15:26:33 -0700
Thanks, Bruce. That's what I thought and this fully clears it up for me. Back to instance methods & properties.
Bruce Wood wrote:
Yes. If the data is static then there is only one of it, and two threads attempting to access the same thing would require some sort of locking, either within the implementation of the object or imposed by the methods that use it.
Just so that you know, I'm no expert on multi-threading. There are others here (such as Jon Skeet) who know far more than I do.
However, I can tell you that threading problems do not have to do with instance methods vs static methods, but have to do with whether two threads are going after the same piece of data at the same time.
(To illustrate further, there is no danger in two threads calling the same instance method in the same object if that method never refers to static variables and never refers to "this." anything--never looks at static state or the object's state. Since the two threads aren't trying to play with the same data items at the same time, no problems.)
In your case, if you have one shared transaction object, you could easily run into the (classic) problem in which the two threads try to both create the transaction object:
if (transaction == null) { transaction = new Transaction(); }
or something like that. If both threads run the "if" test at the same time, they will both discover that there is no transaction object available, and they will both create a new Transaction, the later one clobbering the Tranasaction created by the earlier one.
Again, the problem isn't really that they're running the same code. The problem is that they are testing / manipulating the same (shared) data at the same time.
.
- Follow-Ups:
- Re: static classes & threading
- From: Reginald Blue
- Re: static classes & threading
- References:
- static classes & threading
- From: Frank Rizzo
- Re: static classes & threading
- From: Gabriel Lozano-Morán
- Re: static classes & threading
- From: Frank Rizzo
- Re: static classes & threading
- From: Bruce Wood
- Re: static classes & threading
- From: Frank Rizzo
- Re: static classes & threading
- From: Bruce Wood
- static classes & threading
- Prev by Date: Re: Help! How to cretate a new directory in a given path in C#.NET
- Next by Date: Re: static classes & threading
- Previous by thread: Re: static classes & threading
- Next by thread: Re: static classes & threading
- Index(es):
Relevant Pages
|