Using the CFTHREAD Scope

Did you know that ColdFusion 8 has a new scope? The CFTHREAD scope is a special scope that contains information about threads in the current request. Consider this simple example:

<cfset urls = "http://www.cnn.com,http://www.raymondcamden.com,http://www.yahoo.com">

<cfset counter = 0> <cfloop index=”u” list=”#urls#”> <cfset counter++ > <cfthread theurl=”#u#” name=”thread_#counter#”> <cfhttp url=”#attributes.theurl#”> </cfthread> </cfloop>

<cfdump var=”#cfthread#”> </code>

This code block creates 3 threads, named thread_1, thread_2, and thread_3. (I don’t get paid to be creative!) Each thread created will exist as a structure inside the CFTHREAD scope as demonstrated in this screen shot:

You will notice that not all of my threads are finished. That is because I didn’t do a join on my threads. If you look at Paragator, my ColdFusion RSS Aggregator CFC, you will see I kept a list of all threads so that I could join them. I then used Evalute to create pointers to the data. This can be done a lot easier now. So for example, to join all threads:

<cfthread action="join" name="#structKeyList(cfthread)#" />

And to get access to a thread’s data:

<cfdump var="#cfthread[somethreadname]#">

I’ll be updating Paragator a bit later in the week.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments