A race condition is when multiple requests manipulate the same data at the same time, and the outcome depends on the order of execution. For example, deducting inventory as part of a purchase can provide a race condition:
1) <cfset application.itemCount = application.itemCount - 1 /> This is actually two operations on the same variable (application.itemCount), a read on the right side, and a write on the left side. If two requests execute at the same time, it's possible that both requests will read before either one writes, which will result in an incorrect result. This will be more clear if we break the statement up: 1) <cfset count = application.itemCount /> 2) <cfset count = count - 1 /> 3) <cfset application.itemCount = count /> First, this is the same as my previous example, just that I'm carefully breaking each step down. The same breakdown happens with the first example, just CF/Java is doing it implicitly, and here I'm doing it explicitly. Say application.itemCount starts at 5, and lets run through a sample execution: 1) Request A is received and processes line 1. 2) A processes line 2. 3) Request B is received and processes line 1. 4) A processes line 3. 5) B processes line 2. 6) B proceses line 3. What is the result? application.itemCount will be set to 4, not 3. Here's the values of the variables over the course of the 6 steps: step) 'application.itemCount', A's 'count', B's 'count' // request A is received 1) 5, 5, - 2) 5, 4, - // request B is received 3) 5, 4, 5 4) 4, 4, 5 // request A is over 5) 4, -, 4 6) 4, -, 4 By using an exclusive CFLOCK, we prevent the two requests from executing simultaneously, and thereby avoid the race condition. Request B can't start executing until request A is totally finished. You'll see the obvious drawback, however. Every place you use an exclusive lock, you're effectively single-threading your application, which drastically hurts performance. Therefore it's important to keep locking to a minimum. cheers, barneyb On Apr 5, 2005 7:58 PM, Johnny Le <[EMAIL PROTECTED]> wrote: > What is a race condition? Two threads read the same variable at once? I > heard a lot about race condition lately, but I don't exactly sure what race > condition is. I know that you should use locking to avoid race condition. > It is like doing something to avoid something that I don't even know what it > is. > Johnny -- Barney Boisvert [EMAIL PROTECTED] 360.319.6145 http://www.barneyb.com/ Got Gmail? I have 50 invites. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Logware (www.logware.us): a new and convenient web-based time tracking application. Start tracking and documenting hours spent on a project or with a client with Logware today. Try it for free with a 15 day trial account. http://www.houseoffusion.com/banners/view.cfm?bannerid=67 Message: http://www.houseoffusion.com/lists.cfm/link=i:4:201658 Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4 Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4 Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

