Update 02/23/2013 - Possible bug with server monitor.
This is now an open unverified bug with the server monitor.
Bug ID: 3498550
Just be aware that threads can behave unexpectedly when all server monitoring is turned on.


I ran into a cfthread issue last weekend, and maybe someone can tell me what is going on. The code listed below usually produces the following output:

t1037A344-5056-9852-3BF604C3F789A07E1
t1037A344-5056-9852-3BF604C3F789A07E2
t1037A344-5056-9852-3BF604C3F789A07E3
t1037A344-5056-9852-3BF604C3F789A07E4

struct
HELLO Hello World!

Intermittently I will get the following output:

tE65ACFC2-5056-9852-3BC66FCFAEE291C51
tE65ACFC2-5056-9852-3BC66FCFAEE291C52
tE65ACFC2-5056-9852-3BC66FCFAEE291C53
tE65ACFC2-5056-9852-3BC66FCFAEE291C54
[empty string]

In the application.log, I get an error "tE65ACFC2-5056-9852-3BC66FCFAEE291C51: null"

I have only been able to recreate the problem with the server monitor turned on. I'm wondering if there is a problem with my code.

view plain print about
1<!---
2Tested with the following
3CF Version: 9,0,1,274733
4Update Level: hf901-00008.jar --->

5
6<cfset threads = arrayNew(1)>
7<cfset threadPrefix = createUUID()>
8
9<cffunction name="addThreadName" returntype="string" access="private" output="false">
10    <cfset var threadName = "t" & variables.threadPrefix & arrayLenn(variables.threads) + 1)>
11
12    <cfset arrayAppend(variables.threads, threadName)>
13    <cfreturn threadName>
14</cffunction>
15
16<cfset variables.mystruct = "">
17
18<cfthread name="#addThreadName()#" action="run">
19    <cfthread action="sleep" duration="100" />
20    <cfset variables.mystruct = structNew()>
21    <cfset variables.mystruct.hello = "Hello World!">
22</cfthread>
23
24<cfthread name="#addThreadName()#" action="run">
25</cfthread>
26
27<cfthread name="#addThreadName()#" action="run">
28</cfthread>
29
30<cfthread name="#addThreadName()#" action="run">
31</cfthread>
32
33<cfloop array="#variables.threads#" index="i">
34    <cfoutput>#i#</cfoutput><br />
35 <cfthread action="join" name="#trim(i)#" timeout="2000" / >
36</cfloop>
37
38<cfdump var="#mystruct#" >