Cfthread problems


#1

Hello!

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]="">
            <cfoutput>

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

    <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>
    </cfloop>
</cftimer>

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 5.2.9.31 Error (expression)
Message key [1] doesn’t exist
Stacktrace The Error Occurred in
cfthread.cfm: line 18

16:
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(StructSupport.java:69)
at lucee.runtime.type.StructImpl.get(StructImpl.java:135)
at lucee.runtime.type.util.StructSupport.get(StructSupport.java:244)
at lucee.runtime.util.VariableUtilImpl.get(VariableUtilImpl.java:263)
at lucee.runtime.PageContextImpl.get(PageContextImpl.java:1516)
at marketing.skriptkiste.cfthread_cfm$cf.call(/marketing/skriptkiste/cfthread.cfm:18)

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
Michael


#2

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 …


#3

Hi,

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 https://luceeserver.atlassian.net/browse/LDEV-2150

Thanks,
Michael