Last active
June 30, 2025 16:35
-
-
Save JamoCA/22504a6f3fa47a29fef7fab9944b51ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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 | |
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> | |
</cfloop> | |
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> | |
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()); | |
// 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); | |
} | |
} | |
cfthread action="join" name=arraytolist(threadNames); | |
writedump( request.threadOutput ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment