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

Hello, I am doing mod_perl on linux and win32, and would like to have a mmap cache for the mpm_winnt apache - but Cache::Fastmmap and its WithWin32 extension is not thread-safe AFAIK - I am wondering if there exist a certified thread-safe module for shared memory ? In CPAN, nowhere is mentionned the thread-safety -; Thanks !

Replies are listed 'Best First'.
Re: thread-safe mmap module ?
by kennethk (Abbot) on Feb 21, 2009 at 21:58 UTC
    Never used them myself, but a CPAN search for shared cache brings up a number of promising looking modules, including IPC::SharedCache and Cache::FastMmap. The documentation from Cache::FastMmap says:
    It uses fcntl locking to ensure multiple processes can safely access the cache at the same time.
    so it should certainly be thread-safe.
      I don't recommend using IPC::SharedCache. It's slow, and the author himself (samtregar) suggest using something else these days, as you can see in the reviews.
      There is a concern with the win32 support and Cache::FastMmap warns:

      Cache::FastMmap ./.. will not work on Windows or Win32 like environments.

      and a google search on IPC::SharedCache says something similar ! Unfortunate, I guess I can fall back to a Tie::Cache or a DB backend module for win32 -; Thanks for your answer btw!
        Another result that came up on that page is Cache::FastMmap::WithWin32, which "is a fork of Cache::FastMmap by Rob Mueller to include the Win32 port by Ash Berlin". Once again, never used them, but maybe this would fit your bill?
      It uses fcntl locking to ensure multiple processes can safely access the cache at the same time.
      so it should certainly be thread-safe.

      I'm sorry but why? Aren't fcntl locks shared between threads?

Re: thread-safe mmap module ?
by perrin (Chancellor) on Feb 22, 2009 at 00:56 UTC
    Why don't you use a standard hash shared between the threads? Do you need it to be persistent when you shut down apache?
      Indeed persistency isn't necessary, although supported; you have a point. I see in CPAN the very promising threads-shared module - its .xs clearly proves the win32 support, and in addition it works in non-threaded environments - great suggestion - thank you -;
        You're welcome. There are some other tricks for getting good thread performance with mod_perl, mostly to do with when you load things. If you need more help and suggestions, come visit us on the mod_perl list.
      Indeed.... thats why threads are used .... to facilitate sharing between threads....otherwise just fork. Fork and shared memory are an alternative to threads and threads::shared.

      There may be an advantage to using shared memory in threads, notably it is the fastest IPC out there; but there are how would you control simultaneous concurrent access to the mmap region from different threads? You are risking data corruption.

      I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
        threads:::shared offers a lock() method that is released outside of the {} scope where it is. To try -;
Re: thread-safe mmap module ?
by renodino (Curate) on Feb 22, 2009 at 17:30 UTC
    Based on other comments, you may not really need process-shared memory, but if you still do, you might check IPC::Mmap. It was developed in part to assist in creating a threads-friendly Perl debugger, so it should be thread safe.

    (Hmm, looks like the docs have gotten lost in CPAN's new restriction on package content, you'll have to scroll back to older versions to read the docs)

    Perl Contrarian & SQL fanboy