Learning ColdFusion 8: CFThread's VARIABLES Scope Update (Thanks Dan G. Switzer, II)

Posted June 3, 2007 at 6:34 PM by Ben Nadel

Tags: ColdFusion

The other day, I posted a tutorial on ColdFusion 8's new CFThread tag, specifically, how to get data into and out of the threads. I had misunderstood the documentation when it said that all threads on the page share the same VARIABLES scope. I had taken this to mean each CHILD thread. Thankfully, Dan G. Switzer, II pointed out my misunderstanding and I was able to change the previous CFThread blog post. What Dan pointed out to me was that all the threads do indeed share the same VARIABLES scope, but that the primary page (or parent page) is also included in that pool of threads. Meaning, all threads on a given page request share the same VARIABLES scope.

To demonstrate this concept, take a look at this:

  • <!--- Set a message into the VARIABLES scope. --->
  • <cfset VARIABLES.Message = "From Parent Page" />
  •  
  •  
  • <!--- Launch a child thread. --->
  • <cfthread
  • action="run"
  • name="ThreadOne">
  •  
  • <!---
  • Store a message into this thread's VARIABLES
  • scope. This is NOT the page's VARIABLES scope;
  • threads have their own version.
  • --->
  • <cfset VARIABLES.Message = "From Thread One!" />
  •  
  • </cfthread>
  •  
  •  
  • <!---
  • Let's wait for thread one to finish so that we know
  • exactly where all of our values are coming from.
  • Remember, these threads are asyncronous. In fact, they
  • might not even execute in the same order in which they
  • were defined.
  • --->
  • <cfthread
  • action="join"
  • name="ThreadOne"
  • />
  •  
  •  
  • <!--- Launch a child thread. --->
  • <cfthread
  • action="run"
  • name="ThreadTwo">
  •  
  • <!---
  • Get the message from the VARIABLES scope
  • and store it into this thread's publically
  • accessible THREAD scope.
  • --->
  • <cfset THREAD.Message = VARIABLES.Message />
  •  
  • </cfthread>
  •  
  •  
  • <!---
  • Join the second thread to the current page process.
  • After this, all our child threads will have finished
  • executing (and in the same order as they were declared).
  • --->
  • <cfthread
  • action="join"
  • name="ThreadTwo"
  • />
  •  
  •  
  • <!--- Output the message stored into thread two. --->
  • Thread Two: #ThreadTwo.Message#<br />
  •  
  • <!--- Output the message stored in the parent. --->
  • Parent: #VARIABLES.Message#

Notice that while I am launching two child threads, I am requesting that the parent page wait for each thread to finish processing before it executes the next. This effectively defeats the purpose of the CFThread tag, but it is necessary to show that each thread shares the same VARIABLES scope. Running the above code, we get:

Thread Two: From Thread One!
Parent: From Thread One!

What you can see by the output is that the first thread updates the message value. This change is reflected not only in the message available to the second thread, but also to the parent page.

Thanks Dan, you the man!


You Might Also Be Interested In:



Reader Comments

Jun 3, 2007 at 7:27 PM // reply »
4 Comments

Good notes about ColdFusion 8.
I enjoy reading your articles.
Keep it up.
Pinal Dave


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools