znu has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys (and girls). I've got a threaded program where several threads (hehe, probably around a 100 or so) need to append a line to the same file. Just wondering if someone could point me in the right direction regarding how to go about locking the file so threads do not append to it simultaneously and overwrite each others output. Thanks for your help!

Replies are listed 'Best First'.
Re: Threaded file access
by BrowserUk (Patriarch) on Dec 18, 2002 at 04:28 UTC

    Take a look at the threadsafeThread:Queue module.

    Just create one more thread and have all your other threads enqueue() the data to it and then it dequeue()'s the data and writes it to the log. No locking necessary.

    The synopsis on the link above tells you pretty much all you need to know.


    Examine what is said, not who speaks.

Re: Threaded file access
by PodMaster (Abbot) on Dec 18, 2002 at 03:51 UTC
    Yah.

    Check out our Tutorials section, and especially the tutorial on File Locking.

    Update: If this is for some sort of logging, you might wanna check out some of the numerous LOGging modules oncpan.

     
    I have a different kind of flex ;)

    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Threaded file access
by pg (Canon) on Dec 18, 2002 at 04:07 UTC
    Better to have a server process sitting there, accept requests for output from different threads, and act as a single point for physical write.

    100 threads compete for lock against a single file could significantly decrese performance.

Re: Threaded file access
by submersible_toaster (Chaplain) on Dec 18, 2002 at 05:05 UTC
    ++pg
    ++browserUk

    Beat me to it! but Threads::Queue is the first place I'd suggest you look to solve your problem.

    Just out of interest , what are your 100 threads doing? serving content to multiple connections ? I thought you might refer to UDP Broadcaster to see a working example of using a queue, though now I see that it uses the 5.8 threads, threads::shared::queue , but implementation is similar.