Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active June 30, 2025 16:35
Show Gist options
  • Select an option

  • Save JamoCA/22504a6f3fa47a29fef7fab9944b51ae to your computer and use it in GitHub Desktop.

Select an option

Save JamoCA/22504a6f3fa47a29fef7fab9944b51ae to your computer and use it in GitHub Desktop.

Revisions

  1. JamoCA revised this gist Jun 30, 2025. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,27 @@
    <!--- 6/30/2025 Passing Attributes via CFThread
    <!--- 2025-06-30 Passing Attributes via CFThread
    TWEET: https://x.com/gamesover/status/1939722382379471155

    The offical Adobe ColdFUsion example of passing attributes to a CFThread is not valid. In addition, it's not
    a Short, Self Contained, Correct Example (SSCCE) that can be tested on TryCF.com or CFFiddle.org since it
    requires access to the file system.
    https://helpx.adobe.com/coldfusion/developing-applications/developing-cfml-applications/using-coldfusion-threads/using-thread-data.html#TheAttributesscopeandthreadattributes

    EXAMPLE FROM "Last updated on Jan 13, 2022"
    OFFICAL EXAMPLE: Last updated on Jan 13, 2022
    <cfloop query="dir">
    <cfset threadname = "thread_" & #i#>
    <cfset i=i+1>
    <cfthread name="#threadname#" filename="#dir.name#">
    <cffile action="COPY" source="#src#\#filename#"
    destination="#dest#\#filename#\">
    </cfthread>
    <cfset threadname = "thread_" & #i#>
    <cfset i=i+1>
    <cfthread name="#threadname#" filename="#dir.name#">
    <cffile action="COPY" source="#src#\#filename#" destination="#dest#\#filename#\">
    </cfthread>
    </cfloop>

    This modernized example isn't perfect, but illustrates that "src" & "dest" variables will cause the internal thread
    functionality to silently fail as they were not explicitely passed as attributes in the CFThread tag. (This nuance is
    not communicated. Also, what is "i"? It's not declared. (When iterating over a query object, "currentrow" is a better choice.)
    This modernized example isn't perfect, but illustrates that "src" & "dest" variables will cause internal thread
    functionality to "silently fail" as the unscoped variables were not explicitely passed as attributes in the CFThread tag. (This
    nuance is not communicated.
    Also, what is "i"? It's not declared in the official example. (When iterating over a query object, "currentrow" is a better choice.)

    NOTE: This is just an example to highlight the errors with the official example. It's not advisable (or threadsafe) to use this
    approach to adding values to the request scope... but for this simple test, it "works" to expose the issue.
    --->

    <cfscript>
    @@ -32,6 +36,8 @@ for (row in news){
    arrayappend(threadNames, "thread_" & news.currentrow);
    thread action="run" name="#threadNames[arraylen(threadNames)]#" title=row.title {
    arrayappend(request.threadOutput, title & " " & gettickcount());
    // this line isn't processed because "id" does not exist.
    // If logic is moved above the other arrayappend, a silent fail aborts and nothing is added
    arrayappend(request.threadOutput, id & " " & title);
    }
    }
  2. JamoCA created this gist Jun 30, 2025.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <!--- 6/30/2025 Passing Attributes via CFThread

    The offical Adobe ColdFUsion example of passing attributes to a CFThread is not valid. In addition, it's not
    a Short, Self Contained, Correct Example (SSCCE) that can be tested on TryCF.com or CFFiddle.org since it
    requires access to the file system.
    https://helpx.adobe.com/coldfusion/developing-applications/developing-cfml-applications/using-coldfusion-threads/using-thread-data.html#TheAttributesscopeandthreadattributes

    EXAMPLE FROM "Last updated on Jan 13, 2022"
    <cfloop query="dir">
    <cfset threadname = "thread_" & #i#>
    <cfset i=i+1>
    <cfthread name="#threadname#" filename="#dir.name#">
    <cffile action="COPY" source="#src#\#filename#"
    destination="#dest#\#filename#\">
    </cfthread>
    </cfloop>

    This modernized example isn't perfect, but illustrates that "src" & "dest" variables will cause the internal thread
    functionality to silently fail as they were not explicitely passed as attributes in the CFThread tag. (This nuance is
    not communicated. Also, what is "i"? It's not declared. (When iterating over a query object, "currentrow" is a better choice.)
    --->

    <cfscript>
    news = queryNew("id,title", "integer,varchar", [
    {"id": 1, "title": "Dewey defeats Truman"},
    {"id": 2, "title": "Men walk on Moon"}
    ]);

    threadNames = [];
    request.threadOutput = [];
    for (row in news){
    arrayappend(threadNames, "thread_" & news.currentrow);
    thread action="run" name="#threadNames[arraylen(threadNames)]#" title=row.title {
    arrayappend(request.threadOutput, title & " " & gettickcount());
    arrayappend(request.threadOutput, id & " " & title);
    }
    }

    cfthread action="join" name=arraytolist(threadNames);

    writedump( request.threadOutput );
    </cfscript>