Re: static classes & threading



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.

.



Relevant Pages

  • Re: static classes & threading
    ... static state or the object's state. ... In your case, if you have one shared transaction object, you could ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Use of Queues in System Verilog
    ... The guts of your "push four transactions" loop is... ... Consequently, you have only one transaction object, ... DOULOS - Developing Design Know-how ...
    (comp.lang.verilog)
  • Re: Transactions in ADO.Net 2 strongly-typed datsets.
    ... knowing how to assign the transaction object to the strongly-typed ... me to assign the transaction to the IUD methods of the dataset. ... can't figure out how to do this on this strongly-typed dataset. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Programming to an Interface
    ... the business logic in a transaction would be hidden at the next level down. ... in the above we must assume that Transaction.execute takes the object being modified and saves it to the DB while locking out any concurrent changes. ... Still, the code above says nothing about RDB, tables, rows, or anything else at all about the persistence mechanism. ... So the author has successfully avoided the implementation of the RDB and programmed to the interface of only the Account object, and the Transaction object. ...
    (comp.object)