Chapter 3: Working with Threads

 < Day Day Up > 



Overview

In the previous chapters, we discussed how threads play an important role in developing multiuser applications. We used threads to solve some significant problems, like giving multiple users or clients access to the same resource at the same time. However, in the learning process we ignored one issue that we now need to address: what would happen to the resource if one user changes the state of the resource and at the same time another user wants to change the state of the same resource?

Let's take the example of an ATM. Mr. X and his wife Mrs. X both decide to empty their joint checking account by withdrawing $1,000 from an ATM. Unfortunately they forget to decide who will actually do the job. So, ironically, Mr. X and Mrs. X both access the checking account from different ATMs, at exactly the same time. If two users access the same account at the same time, and if the application is not thread-safe, it may possible that both the ATMs detect that there is enough amount in the checking account and dispense $1,000 to each of their users. The two users are causing two servier-side threads to access the account database at the same time. In an ideal scenario, when one user is trying to update their account, nobody else should have access to that account at that instance. In short, the access to that account should be locked when the user accesses it for updating any information regarding that account.

start sidebar

The .NET Framework provides specific mechanisms to deal with such problems. The phenomenon of allowing only one thread to access a resource at any point of time is called Synchronization. Synchronization is a feature available to developers for creating thread-safe access to critical resources.

end sidebar



 < Day Day Up > 



C# Threading Handbook
C# Threading Handbook
ISBN: 1861008295
EAN: 2147483647
Year: 2003
Pages: 74

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net