Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 338

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
Thread scope variables are only available to the page that created the thread or to other threads created by that page.
No other page can access the data. If one page must access another page's Thread scope data, you must place the data
in a database or file and access it from there.
The Thread scope of each thread is a subscope of a special scope, cfthread, that lasts as long as the request, or until the
last thread that it starts completes, whichever is longer. Thus, if you have two threads, myThread1 and myThread2,
you can access their Thread scopes as cfthread.myThread1 and cfthread.myThread2 until all threads and the request
complete. In most cases, there is no need to use the
name in either of the following situations:
1
If you generate the thread name dynamically, you can avoid using the
scope with associative array notation, as the following code snippet shows:
<cfset threadname="thread_#N#">
...
<!--- The following two lines are equivalent --->
<cfset threadscopeForNthThread = cfthread[threadname] >
<cfset threadscopeForNthThread = Evaluate(threadname) >
If you have a thread with the same name as a Variables scope variable, you can access that thread's Thread scope
2
only by prefacing the Thread name with
error.
The Attributes scope and thread attributes
The Attributes scope contains attributes that are passed to the thread, either individually or in the
attribute. The Attributes scope is available only within the thread and only for the life of the
attributeCollection
thread.
ColdFusion makes a complete (deep) copy of all the attribute variables before passing them to the thread; therefore,
the values of the variables inside the thread are independent of the values of any corresponding variables in other
threads, including the page thread. For example, if you pass a CFC instance as an attribute to a thread, the thread gets
a complete new copy of the CFC, including the contents of its This scope at the time that you create the thread. Any
changes made to the original CFC outside the thread, for example, by calling a CFC function, have no effect on the
copy that is in the thread. Similarly, any changes to the CFC instance in the thread have no effect on the original CFC
instance.
Copying the data ensures that the values passed to threads are thread-safe, because the attribute values cannot be
changed by any other thread. If you do not want duplicate data, do not pass it to the thread as an attribute or in the
attribute. Instead, keep the data in a scope that the thread can access. An example of an object
attributeCollection
that should not be passed to the thread as an attribute is a singleton CFC that should never be duplicated. The singleton
CFC must be kept in some shared scope and accessed by threads. For more information, see the
on page 334.
Because ColdFusion copies all attributes by value, you can have multiple threads, for example, threads created
dynamically in a loop, that use the same attribute names, but where each thread gets a different value, as shown in the
following code excerpt, which creates separate threads to copy each of several files in a directory:
<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>
scope directly. However, you can use the cfthread scope
cfthread
. Otherwise, you access the Variables scope variable, or get an
cfthread
Last updated 1/20/2012
function by using the
Evaluate
"Using other
333
cfthread
scopes"

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents