Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 637243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:36:10+00:00 2026-05-13T20:36:10+00:00

I am porting some code from windows to Linux (Ubuntu 9.10). I have a

  • 0

I am porting some code from windows to Linux (Ubuntu 9.10). I have a simple class (please see below), which uses windows functions to implement simple mutex locking. I want to use Boost.Threads to reimplement this, but that library is new to me.

Can someone point out the changes I need to make to the class below, in order tomuse Boost.Threads instead of the WIN specific functions?

#ifndef __my_READWRITE_LOCK_Header__
#define __my_READWRITE_LOCK_Header__

#include <windows.h>

//Simple RW lock implementation is shown below. 

#define RW_READERS_MAX 10
#define RW_MAX_SEMAPHORE_COUNT 10
#define RW_MUTEX_NAME       L"mymutex"
#define RW_SEMAPHORE_NAME   L"mysemaphore"
class CThreadRwLock
{
public:
    CThreadRwLock()
    {
        InitializeCriticalSection(&m_cs);
        m_hSem = CreateSemaphore(0, RW_READERS_MAX, RW_READERS_MAX, 0);     
    }

    ~CThreadRwLock()
    {
        DeleteCriticalSection(&m_cs);
        CloseHandle(m_hSem);
    }

    void AcquireReaderLock()
    {
        EnterCriticalSection(&m_cs);
        WaitForSingleObject(m_hSem, INFINITE);
        LeaveCriticalSection(&m_cs);
    }

    void AcquireWriterLock()
    {
        EnterCriticalSection(&m_cs);
        for(int i = 0; i < RW_READERS_MAX; i++)
        {
            WaitForSingleObject(m_hSem, INFINITE);          
        }
        LeaveCriticalSection(&m_cs);
    }

    void ReleaseReaderLock()
    {
        ReleaseSemaphore(m_hSem, 1, 0);
    }


    void ReleaseWriterLock()
    {
        ReleaseSemaphore(m_hSem, RW_READERS_MAX, 0);
    }

private:
    CRITICAL_SECTION    m_cs;
    HANDLE              m_hSem;
};




class CProcessRwLock
{
public:
    CProcessRwLock()
    {
        m_h = CreateMutex(NULL, FALSE, RW_MUTEX_NAME);
        m_hSem = CreateSemaphore(NULL, RW_MAX_SEMAPHORE_COUNT, RW_MAX_SEMAPHORE_COUNT, RW_SEMAPHORE_NAME);
    }

    ~CProcessRwLock()
    {
        CloseHandle(m_h);
    }

    void AcquireReaderLock()
    {
        WaitForSingleObject(m_h, INFINITE);
        ReleaseMutex(m_h);
    }

    void AcquireWriterLock()
    {
        WaitForSingleObject(m_h, INFINITE);
        for(int i = 0; i < RW_READERS_MAX; i++)
        {
            WaitForSingleObject(m_hSem, INFINITE);          
        }
        ReleaseMutex(m_h);
    }

    void ReleaseReaderLock()
    {
        ReleaseSemaphore(m_hSem, 1, 0);
    }


    void ReleaseWriterLock()
    {
        ReleaseSemaphore(m_hSem, RW_READERS_MAX, 0);
    }

private:
    HANDLE              m_h, m_hSem;
};


class AutoThreadRwLock
{
public:
    AutoThreadRwLock(const bool readlock = true):m_readlock(readlock)
    {
        if (readlock)
            m_lock.AcquireReaderLock();
        else
            m_lock.AcquireWriterLock();
    }

    ~AutoThreadRwLock()
    {
        if (m_readlock)
            m_lock.ReleaseReaderLock();
        else
            m_lock.ReleaseWriterLock();
    }

private:
    AutoThreadRwLock(const AutoThreadRwLock&);
    AutoThreadRwLock& operator= (const AutoThreadRwLock& );

    CThreadRwLock m_lock ;
    bool m_readlock ;
};


class AutoProcessRwLock
{
public:
    AutoProcessRwLock(const bool readlock = true): m_readlock(readlock)
    {
        if (readlock)
            m_lock.AcquireReaderLock();
        else
            m_lock.AcquireWriterLock();
    }

    ~AutoProcessRwLock()
    {
        if (m_readlock)
            m_lock.ReleaseReaderLock();
        else
            m_lock.ReleaseWriterLock();
    }

private:
    AutoProcessRwLock(const AutoProcessRwLock&);
    AutoProcessRwLock& operator= (const AutoProcessRwLock&);

    CProcessRwLock m_lock ;
    bool m_readlock ;
};

#endif //__my_READWRITE_LOCK_Header__
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:36:10+00:00Added an answer on May 13, 2026 at 8:36 pm

    I’m not going to re-write all your code for you. However you should look into boost’s shared_mutex class.

    Also, this question from StackOverflow shows how to use a boost::shared_mutex

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm porting some code from BSD sockets to Winsock, and I'm not sure how
Recently, when porting some STL code to VS2008 I wanted to disable warnings generated
I'm in the process of porting some ANSI C++ code to C#... and this
I am porting some queries from Access to T-SQL and those who wrote the
I'm porting some MATLAB functions to Scilab . The cool thing is that there
While porting an application from SQL 2005 to SQL Server Compact Edition, I found
While porting a desktop application to windows mobile I've reached the following error: Error
Im porting a project from php to java. The project is a web-app based
Posting a stack overflow question on stackoverflow.com, how amusing :-) I'm running some recursive
I'm working on porting a Visual C++ application to GCC (should build on MingW

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.