Charles Robertson
Charles Robertson

Reputation: 1820

Can I queue a cfthread with the same name in a multi user environment?

User A fires off a cfthread called 'thread1'. This thread takes 30 minutes to complete, and is a 'set & forget' thread. In other words, the thread does not join back with the main page request. The thread contains routines that are highly memory intensive.

User B then fires off the same named thread [cfthread with the name 'thread1'], but from a different page request, 5 minutes after User A.

In this scenario, how can I queue the threads, so that I can reduce the processing load on the CF Application server?

Please note, that I understand about thread queuing with threads that have different names. I am talking about instances of the same thread.

Upvotes: 2

Views: 265

Answers (1)

Charles Robertson
Charles Robertson

Reputation: 1820

The answer is to lock the function call inside the cfthread tag. Here is an example:

<cfthread action="run" name="thread1">
  <cflock name="threadlock" type="exclusive" timeout="10000">
    <cfset callToSomeFunction()>
  </cflock>
</cfthread>

So, to test this, copy the code below, into a .cfm template. Open up Firefox, and open up Chrome. Then test the template inside Firefox. Wait 5 seconds and then test the template inside Chrome:

<cfthread action="run" name="thread1">
  <cfset tickstart = GetTickCount()>
  <cfset time = StructNew()>
  <cfset time.timestart = DateFormat(now(),'dd-mm-yy') & " " & TimeFormat(now(),'hh-mm-ss')>
  <cflock name="threadlock" type="exclusive" timeout="10000">
    <cfthread action="sleep" duration="#(10 * 1000)#" />
  </cflock>
  <cfset tickend = GetTickCount()> 
  <cfset tick = tickend - tickstart> 
  <cfset time.tick = tick/1000>
  <cfset time.timeend = DateFormat(now(),'dd-mm-yy') & " " & TimeFormat(now(),'hh-mm-ss')>
  <cfdump var="#time#" format="html" metainfo="no" output="somefilepath\#thread1.name#-#DateFormat(now(),'dd-mm-yy')#-#TimeFormat(now(),'hh-mm-ss')#.htm" />
</cfthread>

Upvotes: 2

Related Questions