Skip to main content
WolfShade
Legend
July 28, 2009
Question

CFLOCK and CFDIRECTORY

  • July 28, 2009
  • 3 replies
  • 1638 views

Hello, all.

If I put an exclusive CFLOCK around a CFDIRECTORY, will that prevent anything else from accessing the directory until I close the CFLOCK?

Thanks,

^_^

    This topic has been closed for replies.

    3 replies

    Inspiring
    July 30, 2009

    A <cflock> will reliably block any other code which attempts to lock the same name, within the same scope, at the same time.

    "That's it.  It locks a name within a scope.  That's it. That's all." It not matter what the code inside of the lock-protected section is doing.  What matters is that every piece of code that is "doing the same thing," or that is accessing the same shared resource in any way whatever, must be enclosed within <cflock> tags which are locking the same name within the same scope.

    If there exists any code, anywhere, which touches that shared resource (a file, a variable, or whatever it may be) in any way whatever without doing so within the auspices of a correctly-named and correctly-scoped lock ... it is a most serious bug that can quite-easily escape your attempts at testing.

    If you need to make sure that the shared resource is stable during your access to it, but you don't mind if other pieces of code are examining it at the same time as you, then use a "read" lock.  But if you intend to modify the shared resource in any way, your lock must be "exclusive."  You must be mindful of the possibility of deadlock, or, separately, lock-timeout.

    To write this kind of code correctly, first familiarize yourself ("Google it...") with the issues of so-called "mutual exclusion," then carefully plan your locking strategy and hand-inspect the completed code to make certain that it complies with your strategy.

    Inspiring
    July 30, 2009

    The <cflock> tag uses a named-mutex implementation.  All applications and pages must cooperatively make sure that they are conforming to whatever mutual-exclusion arrangement you have devised.  You will have to "desk check" your code.

    WolfShade
    WolfShadeAuthor
    Legend
    July 30, 2009

    So, for example, if I have one CFM file that has the following code:

    <cflock name="dirLock" type="exclusive">

      <cffile action="write" addnewline="no" file="D:\data\text\ids\thisfile.txt" output="#thisOutputString#">

    </cflock>

    .. and then have another CFM file that has:

    <cflock name="dirLock" type="exclusive">

      <cffile action="read" file="D:\data\text\ids\thisfile.txt" variable="dataString">

    </cflock>

    .. and let's say that the file in question is 3 megabytes in size.  If either one of the CFM files is accessing the directory, will that lock out the other file until the first file is done processing?

    Thanks,

    ^_^

    Participating Frequently
    July 30, 2009

    Yes, make sure you add timeouts to your cflocks too.. so they don't wait forever to try to get a lock.  Of course you will want to add error handling if the locks do timeout.

    Participating Frequently
    July 28, 2009

    It will only prevent that block of code from executing while the lock is in place.

    If you have multiple places where to code to manipulate the directory is used, make sure you use a named lock.

    HTH,

    Kevin