I'm currently struggling to get to grips with threads. I have a feeling it might be to do with my scopes; however, I can't see where I am going wrong with this.
My CFC contains the function below:
<cfcomponent output="false" hint="thread stuff.">
<cffunction name="threadTest" access="public" returntype="struct">
<cfscript>
local.lstOne = "1,2,3,4,5,6";
local.a = [];
local.s = {};
local.lst = "";
for(local.x = 1; local.x lte listlen(local.lstOne,','); local.x++){
local.lst &= (len(local.lst) gt 0) ? ',thr#local.x#' : 'thr#local.x#';
thread action="run" name="thr#local.x#" nIndex="#local.x#" aArray="#local.a#"{
thread.y = attributes.nIndex;
thread.aArray = attributes.aArray;
if(thread.y mod 2){
thread.c = 1;
} else {
thread.c = 0;
}
thread.stArgs = {};
thread.stArgs.nMod = thread.c;
arrayAppend(thread.aArray, thread.stArgs);
}
}
threadJoin(local.lst);
local.s.counts = local.a;
return local.s;
</cfscript>
</cffunction>
</cfcomponent>
and I have a CFM page that looks a little like this:
<cfscript>
theThread = createObject( "component", "ThreadStuff" ).init();
theThread.threadTest();
</cfscript>
When I run this, coldfusion comes back with the error Element X is undefined in LOCAL..
I can't work out why it is losing local.x after the first iteration of the loop (I have proven this by doing a dump at the beginning of the loop and at the end of the loop, and it can't get to local.x = 2).
where might I be going wrong?
Coldfusion 9.0.0 (version used in this question: 9,0,0,251028) has a bug where the local scope breaks when a thread is used within a loop within a function.
This issue is fixed in Coldfusion 9.0.1, see details here: http://helpx.adobe.com/coldfusion/kb/issues-fixed-coldfusion-9-0.html id:80153.