Cfthread problems


I’m trying to improve loading time by using cfthread.
Starting with an Adobe example I produced following piece of code:

<cftimer label="Debug" type="outline">
    <cfloop index="pageIndex" from="1" to="10">
        <cfthread name="thr#pageIndex#" threadIndex="#pageIndex#" action="run">
            <cfset Variables.sThreadOutput[threadIndex]="">

                <cfsavecontent variable="Variables.sThreadOutput[threadIndex]">#Variables.sThreadOutput[threadIndex]#
                    Thread index attribute: #threadIndex#  Page index value:  #pageIndex#

    <cfthread action="join" name="thr1,thr2,thr3,thr4,thr5,thr6,thr7,thr8,thr9,thr10" timeout="3000"/>

    <cfloop index="j" from="1" to="10">
        <cfoutput>#Variables.sThreadOutput[j]# <br /></cfoutput>
        <cfset threadname="thr"&j>

I somehow expected that the pageIndex and threadIndex values would be the same in the gereated output. Somehow they are not. Maybe someone can explain to me, why they are different.

Sometimes I get exceptions when loading the file for the first time. Why does this happen?

Lucee Error (expression)
Message key [1] doesn’t exist
Stacktrace The Error Occurred in
cfthread.cfm: line 18

17: <cfloop index=“j” from=“1” to=“10”>
18: <cfoutput>#Variables.sThreadOutput[j]# <br /></cfoutput>
19: <cfset threadname=“thr”&j>
20: </cfloop>
Java Stacktrace lucee.runtime.exp.ExpressionException: key [1] doesn’t exist
at lucee.runtime.type.util.StructSupport.invalidKey(
at lucee.runtime.type.StructImpl.get(
at lucee.runtime.type.util.StructSupport.get(
at lucee.runtime.util.VariableUtilImpl.get(
at lucee.runtime.PageContextImpl.get(
at marketing.skriptkiste.cfthread_cfm$

Is there a smarter way to get the generated output from cfthread. I have problems addressing the “OUTPUT” variable within the cfthread-struct.

Slighly off-topic: Neither cffiddle nor trycf can run the sourcecode above. Are there known limitations on that platforms?

Thanks & regards

Hi @Micha11,

I’ve analyzed your code. Its interesting!.
From my side, First thread runs independently. No need to wait for anything. All of them executed in parallel. I think inner loop is first executed before the outer loop. For every time, value may overwritten by other thread followed by the execution flow (but not sure about this), So only the page index value is differ for every thread. Error also occurred as the thread may not be executed. Manually we can’t able to predict the thread execution which one executed first & last.
If you dump the cfthread / variables you may see the execution of threads in different way. Every thread have different time for execution, status may differ, order of execution also not in proper way …


thank you for your reply. I played around with some more cfthread things and installed a version of ACF for testing as well.
Basically, everything seems to work as expected and it is almost the same in ACF. I wasn’t aware that the variables scope is shared within all threads in one request.

So only the exception I get sometimes seems to be a bug. Maybe it is related to