You're right again. Fu*king sh*t. It's been a horrible day for writing
code in Bellingham. But then I guess that's why I work for a little company
up here rather than Macromedia.
Use a semaphore. Ok, this time I copied and pasted from actual code that is
confirmed to work correctly (rather that writing it on the fly). No
single-threading (except before the first request after app startup finished
the exclusive lock), and no need to chain. It also handles multiple related
objects that must be constructed together (usually the case, in my
experience).
<cflock scope="application" type="readonly" timeout="10">
<cfset reload_app_vars = NOT structKeyExists(application,
"app_vars_loaded") />
</cflock>
<cfif reload_app_vars>
<cflock scope="application" type="exclusive" timeout="10">
<cfif NOT structKeyExists(application, "app_vars_loaded")>
<cfset application.foo = createObject("component", "foo") />
<cfset application.foo.init() />
<cfset application.app_vars_loaded = true />
</cfif>
</cflock>
</cfif>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Sean A Corfield
> Sent: Tuesday, March 16, 2004 2:43 PM
> To: [EMAIL PROTECTED]
> Subject: Re: [CFCDev] Returing "THIS" from init(), WAS: RFC,
> CFC Best Practices
>
> On Mar 16, 2004, at 2:33 PM, Barney Boisvert wrote:
> > <cflock type="readonly">
> > <cfset reload = NOT structKeyExists(application, "foo") />
> > </cflock>
> > <cfif reload>
> > <cflock type="exclusive">
> > <cfif NOT structKeyExists(application, "foo")>
> > <cfset application.foo = createObject("component", "foo") />
> > <cfset application.foo.init() />
> > </cfif>
> > </cflock>
> > </cfif>
>
> That code is not thread safe either (for exactly the same reason).
> Thread 1 can get as far as creating foo but not initializing it and
> then thread 2 can run off and try to use the uninitialized foo
> object...
>
> Regards,
> Sean
>
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> in the message of the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
>
> An archive of the CFCDev list is available at
> www.mail-archive.com/[EMAIL PROTECTED]
>
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]