Click here to Skip to main content
15,883,705 members
Articles / Desktop Programming / MFC

Inter Computer Read/Write File Lock

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
4 Jan 2010CPOL5 min read 34.6K   502   14  
Using File Management Windows API for implementing inter computer Read/Write lock
/*!
\file RWFileLock.h
\brief Header file for a Read/Write File Lock classes.

Implements inter computer read/write locks.
\note These classes are not protected from multithreaded access
      because they are intended to be created on stack or as members of other classes
      that are created on stack.
\author Andriy Brozgol
*/

#pragma once

/*!
\brief Multi threading.
*/
namespace NMt
{
  /*!
  \brief A Read/Write File Lock implementation base class.
  */
  class CRWFileLock
  {
  public:
    // LIFECYCLE
    /*!
    \brief Constructor.
    \param[in] xi_bIsReadLock if it is a read lock
    \param[in] xi_cszFilePath a path to a file to be accessed
    \param[in] xi_bInitialLock if it is initially locked
    \param[in] xi_nPollPeriodMs polling period (milliseconds)
    */
    CRWFileLock(bool xi_bIsReadLock, LPCTSTR xi_cszFilePath, bool xi_bInitialLock = false, 
      DWORD xi_nPollPeriodMs = 1000);
    /*!
    \brief Destructor.
    */
    ~CRWFileLock();

    // OPERATIONS
    /*!
    \brief Locks access to the file.
    */
    void Lock();
    /*!
    \brief Unlocks access to the file.
    */
    void Unlock();

  protected:
    // DATA MEMBERS
    /*!
    \brief Readers/Writers lock file path.
    */
    CString m_sReaderWriterLockFilePath;
    /*!
    \brief Writers lock file path.
    */
    CString m_sWriterLockFilePath;
    /*!
    \brief Readers/Writers lock file.
    */
    HANDLE m_hReaderWriterLockFile;
    /*!
    \brief Writers lock file.
    */
    HANDLE m_hWriterLockFile;
    /*!
    \brief If it is locked.
    */
    bool m_bIsLocked;
    /*!
    \brief If it is a read lock.
    */
    bool m_bIsReadLock;
    /*!
    \brief Polling period (milliseconds).
    */
    DWORD m_nPollPeriodMs;

  };

  /*!
  \brief Read File Lock class.
  */
  class CReadFileLock : public CRWFileLock
  {
  public:
    // LIFECYCLE
    /*!
    \brief Constructor.
    \param[in] xi_cszFilePath a path to a file to be accessed
    \param[in] xi_bInitialLock if it is initially locked
    \param[in] xi_nPollPeriodMs polling period (milliseconds)
    */
    CReadFileLock(LPCTSTR xi_cszFilePath, bool xi_bInitialLock = false, DWORD xi_nPollPeriodMs = 1000) :
        CRWFileLock(true, xi_cszFilePath, xi_bInitialLock, xi_nPollPeriodMs) {}
  };
  /*!
  \brief Write File Lock class.
  */
  class CWriteFileLock : public CRWFileLock
  {
  public:
    // LIFECYCLE
    /*!
    \brief Constructor.
    \param[in] xi_cszFilePath a path to a file to be accessed
    \param[in] xi_bInitialLock if it is initially locked
    \param[in] xi_nPollPeriodMs polling period (milliseconds)
    */
    CWriteFileLock(LPCTSTR xi_cszFilePath, bool xi_bInitialLock = false, DWORD xi_nPollPeriodMs = 1000) :
        CRWFileLock(false, xi_cszFilePath, xi_bInitialLock, xi_nPollPeriodMs) {}
  };

  /*!
  \brief Displays a message.
  \param[in] xi_cszFormat a message format
  */
  void DisplayMsg(LPCTSTR xi_cszFormat, ...);

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
I am a professional C++/C# developer. In my free time I also develop for Windows Phone. Please see my applications at Windows Store

Comments and Discussions