When creating threads in a loop, it appears that each thread is overwriting the previous. Here is some pseudo code:
<cfscript>
LOCAL.Items=[
"ABC",
"DEF",
"GHI",
"JKL",
"MNO",
"PQR",
"STU",
"VWX",
"YZ"];
for (LOCAL.item in LOCAL.Items) {
writeoutput(LOCAL.item & "<br />");
}
for (LOCAL.item in LOCAL.Items) {
thread name="items-#LOCAL.item#" action="run" {
writelog(text=LOCAL.item, type="information", file="thread-test");
}
}
</cfscript>
Output of first loop:
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
(as expected)
Data in thread-test.log:
"Information","cfthread-23","03/11/24","13:18:40","MNO"
"Information","cfthread-28","03/11/24","13:18:40","YZ"
"Information","cfthread-23","03/11/24","13:18:40","YZ"
"Information","cfthread-28","03/11/24","13:18:40","YZ"
"Information","cfthread-23","03/11/24","13:18:40","YZ"
"Information","cfthread-28","03/11/24","13:18:40","YZ"
"Information","cfthread-23","03/11/24","13:18:40","YZ"
"Information","cfthread-28","03/11/24","13:18:40","YZ"
"Information","cfthread-23","03/11/24","13:18:40","YZ"
Shouldn't naming each thread uniquely create a unique thread and use the current value of LOCAL.item
?
What am I missing here?
Did a little more poking around and found that cfthread/thread can accept custom attributes which can be accessed via the ATTRIBUTES scope. This seems to work:
for (LOCAL.item in LOCAL.Items) {
thread name="items-#LOCAL.item#" action="run" item=LOCAL.item {
writelog(text=ATTRIBUTES.item & "<br />", type="information", file="thread-test");
}
}
Contents of thread-test.log:
"Information","cfthread-29","03/11/24","13:48:35","ABC" "Information","cfthread-31","03/11/24","13:48:35","GHI" "Information","cfthread-32","03/11/24","13:48:35","JKL" "Information","cfthread-30","03/11/24","13:48:35","DEF" "Information","cfthread-33","03/11/24","13:48:35","MNO" "Information","cfthread-33","03/11/24","13:48:35","STU" "Information","cfthread-32","03/11/24","13:48:35","YZ" "Information","cfthread-34","03/11/24","13:48:35","PQR" "Information","cfthread-30","03/11/24","13:48:35","VWX"