I've 'solved' it as such - but I'll put in the extra time. :o)

notThreadSafe.cfc
--
<cfcomponent>
   <!--- pseudo constructor --->
   <cfscript>
       instance.myArray = arrayNew(1);
   </cfscript>
   
   <!--- 
        Add an item to the CFC
    --->
   <cffunction name="add" access="public" returntype="void">
        <cfargument name="obj" type="any" required="yes">       
                <cfscript>
                        ArrayAppend(instance.myArray, argument.obj);
                </cfscript>     
   </cffunction>
   
   <!--- 
        Remove an item from the CFC
    --->   
   <cffunction name="delete" access="public" returntype="void">
        <cfargument name="index" type="numeric" required="Yes"> 
                <cfscript>
                        ArrayDeleteAt(instance.myArray, arguments.index);
                </cfscript>     
   </cffunction>
   
   <!--- 
        gets an item from the CFC
    --->   
   <cffunction name="get" access="public" returntype="void">
        <cfargument name="index" type="numeric" required="Yes"> 
                <cfscript>
                        return instance.myArray[index];
                </cfscript>     
   </cffunction>   


</cfcomponent>
--

Application.cfm
--
<cfapplication name="notthreadsafe">
<cfif NOT isDefined("application.notThreadSafe")>
        <cfset application.notThreadSafe = createObject("component", "notThreadSafe")>
</cfif>
--

setup.cfm
--
<cfscript> 
                //can assume notThreadSafe is in application scope.
        application.notThreadSafe.add("fred");
        application.notThreadSafe.add("dog");
        application.notThreadSafe.add("yoda");
        application.notThreadSafe.add("stuff");
        application.notThreadSafe.add("another");       

</cfscript>
--

runthis.cfm
--
<cfscript>
                //can assume notThreadSafe is in application scope.
        str = application.notThreadSafe.get(5);
        application.notThreadSafe.delete(5);
        application.notThreadSafe.add(str);
</cfscript>   
--

Now obviously, I've not included any locking in any places.

(none of this code is tested, but it SHOULD work).

Run setup.cfm
The run runthis.cfm AT THE SAME TIME as another computer is also running it.

There is no way that you can guarentee data safety on the CFC - in fact I'm sure it 
will error, but only when two 
threads collide.

Obviously there should be some locking at SOME level, to prevent this from happening.  
However, should the 
locking be at the level internal to the CFC (which I would advocate, except as a 
NAME'd lock, rather than a 
SCOPE lock), or should it be at 'runthis.cfm' as a scoped locking?

This is the option I would advocate:

threadSafe.cfc
--
<cfcomponent>
   <!--- pseudo constructor --->
   <cfscript>
       instance.myArray = arrayNew(1);
   </cfscript>
   
   <!--- 
        Add an item to the CFC
    --->
   <cffunction name="add" access="public" returntype="void">
        <cfargument name="obj" type="any" required="yes">       
                <cflock timeout="60" throwontimeout="Yes" name="threadSafe.lock" 
type="EXCLUSIVE">
                <cfscript>
                        ArrayAppend(instance.myArray, argument.obj);
                </cfscript>     
                </cflock>
   </cffunction>
   
   <!--- 
        Remove an item from the CFC
    --->   
   <cffunction name="delete" access="public" returntype="void">
        <cfargument name="index" type="numeric" required="Yes"> 
                <cflock timeout="60" throwontimeout="Yes" name="threadSafe.lock" 
type="EXCLUSIVE">
                <cfscript>
                        ArrayDeleteAt(instance.myArray, arguments.index);
                </cfscript>     
                </cflock>
   </cffunction>
   
   <!--- 
        gets an item from the CFC
    --->   
   <cffunction name="get" access="public" returntype="void">
        <cfargument name="index" type="numeric" required="Yes"> 
                <cflock timeout="60" throwontimeout="Yes" name="threadSafe.lock" 
type="EXCLUSIVE">
                        <cfset var obj = instance.myArray[index]>
                </cflock>
                <cfscript>
                        return obj;
                </cfscript>     
   </cffunction>   


</cfcomponent>
--

Hopefully this makes sense at this point.

Mark
------------------------------------------------------------------
[EMAIL PROTECTED]
ICQ: 3094740
Safe From Bees
[ www.safefrombees.com ]


Quoting Stephen Bosworth <[EMAIL PROTECTED]>:

> Mark,
> 
> I was reading what you sent, in fact, I was discussing it here with a couple
> of fellow workers.
> 
> We are obviously at a lost as to what you are trying to explain/discuss
> here.
> 
> Can you provide a code example of people like me you cannot understand what
> you are trying to acheive can gain an idea as to what you are trying to do
> exactly.
> 
> Cheers
> 

---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]

MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia
http://www.mxdu.com/ + 24-25 February, 2004

Reply via email to