views:

37

answers:

1

Hi all, please consider the following:

  • I have a queue of objects represented as an array.

  • I process them off the top of the array (at position 1) before calling arrayDeleteAt() to remove it from the array.

  • I add new queue item at the top of the array using arrayAppend().

This works fine. However, I now wish to re-order the array immediately after adding an item.

I am concerned that if a thread is taking from the queue it will find the queue order has changed between it taking the item at position 1 and it deleting the item at position 1 - because in that time an additional item has been added the the queue has been re-sorted. So I need to ensure my queue is thread-safe.

Is there any way to doing this using the cflock tag? Since my add and remove code are in different places in the code the thread executing one bit of code would need to know that a thread is executing another specific bit of code and halt until that other thread has stopped executing it's code.

Or am I better off just raising a flag while the sorting is going on and preventing anything being taken from the array while the sort is in progress?

All this is happening in the APPLICATION scope on a CF 8 Enterprise server.

Thanks in advance for any help.

Ciaran

+3  A: 

An exclusive CFLOCK should do what you want. You could just scope-lock APPLICATION, but that might be overly broad. Probably best to do it as a named lock. It won't matter where the different bits of code with the lock are located, as long as they're all using the same name.

Al Everett
Thanks. I didn't know what you could have more than one lock with the same name - I was really hoping this would be the case :)
Ciaran Archer
@ciaranarcher: Better choose and unambiguous name. Named locks are server-wide!
Tomalak