2

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.

Charles Robertson
  • 1,760
  • 16
  • 21
  • You could give them dynamic names. Instead of 'thread1', make it 'thread#cfid##cftoken##gettickcount()#'. – Dan Bracuk Nov 01 '15 at 15:43
  • I don't want them to have different names. I need to fire the same thread from different page requests. I think I have found the answer. I need to lock the routine inside the thread. – Charles Robertson Nov 01 '15 at 21:00

1 Answers1

2

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>
Charles Robertson
  • 1,760
  • 16
  • 21