2

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?

Jarede
  • 3,310
  • 4
  • 44
  • 68
  • I ran this without error on CF 9 (9,0,2,282541). It returned the `counts` struct with an empty `a` array. – imthepitts Feb 19 '13 at 20:04
  • hmmm it should really return the struct with the arrays with the structs in from the threads... but thats probably more my code being wrong, the main issue for me is the local.x not exisiting... not sure how to debug that issue. – Jarede Feb 19 '13 at 22:16
  • Is this the exact code? It initially failed for me because there was no `init()` function in the CFC. If this is an abridged version, please edit with the exact code, and hopefully I'll be able to repro your error. – imthepitts Feb 19 '13 at 22:32
  • it's not the exact code... the init doesn't have anything that could affect this... the application.cfc might though, which is harder to disect and paste – Jarede Feb 19 '13 at 23:05
  • 1
    Not sure what to tell you about the `local.x` variable being undefined, but this code won't do what you're hoping. The `local.s` structure only contains one item, which is an empty array `local.a`. Currently, `local.a` is being passed in as empty for each thread and then it is *not modified* within the thread. So it comes out as empty still. To get the struct to return the counts from the threads, you'll need to pass that struct into the thread and append the arrays to it within the thread block. – imthepitts Feb 19 '13 at 23:41

2 Answers2

1

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.

Jarede
  • 3,310
  • 4
  • 44
  • 68
0

If the problem is that variable local.x is not incrementing, start by commenting out all the threading stuff. Replace it with a writedump of local. Writedump the local scope before and after the loop as well.

Once you get local.x incrementing, add empty threads. Continue to writedump the local scope so you can see if this is what causes the problem. If local.x is still incrementing, add very small bits of code until you find the bit that causes the problem.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43