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

  • SEARCH
  • Home
  • 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 6763181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:26:44+00:00 2026-05-26T14:26:44+00:00

I cannot use boost or the latest std::thread library. The way to go is

  • 0

I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex.

In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked.

Any implementation available? I don’t want to re-invent the wheel.

I need to use pthreads.

  • resource acquisition is initialization == “RAII”
  • 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-26T14:26:45+00:00Added an answer on May 26, 2026 at 2:26 pm

    Note This is an old answer. C++11 contains better helpers that are more platform independent:

    • std::lock_guard
    • std::mutex, std::timed_mutex, std::recursive_mutex, std::recursive_timed_mutex

    And other options like std::unique_lock, boost::unique_lock

    Any RAII tutorial will do.

    Here’s the gist: (also on http://ideone.com/kkB86)

    // stub mutex_t: implement this for your operating system
    struct mutex_t 
    { 
        void Acquire() {} 
        void Release() {} 
    };
    
    struct LockGuard
    {
         LockGuard(mutex_t& mutex) : _ref(mutex) 
         { 
             _ref.Acquire();  // TODO operating system specific
         }
    
         ~LockGuard() 
         { 
              _ref.Release(); // TODO operating system specific
         }
       private:
         LockGuard(const LockGuard&); // or use c++0x ` = delete`
    
         mutex_t& _ref;
    };
    
    int main()
    {
        mutex_t mtx;
    
        {
            LockGuard lock(mtx);
            // LockGuard copy(lock); // ERROR: constructor private
            // lock = LockGuard(mtx);// ERROR: no default assignment operator
        }
    
    }
    

    Of course you can make it generic towards mutex_t, you could prevent subclassing.
    Copying/assignment is already prohibited because of the reference field

    EDIT For pthreads:

    struct mutex_t
    {
        public:
            mutex_t(pthread_mutex_t &lock) : m_mutex(lock) {}
    
            void Acquire() { pthread_mutex_lock(&m_mutex);   }
            void Release() { pthread_mutex_unlock(&m_mutex); }
        private:
            pthread_mutex_t& m_mutex;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have gcc 4.4.5 and the latest boost library. I want to use boost
Is there a way to use boost foreach without defining a const_iterator ? My
We use boost - so using that library should be fine. But I've never
Lately I read this post: How do I use boost.lambda with boost.thread to get
I am relatively new to CMake, and I'm trying use the boost asio library
I am currently writing a CUDA application and want to use the boost::program_options library
std::auto_ptr lacks const copy constructor, therefore I cannot use it directly in collections. is
I want to use the boost library in a c++ project I'm doing in
I'm attempting to use the boost library in my C++ project (Visual Studio 2008).
I cannot use the Ctrl + F and Ctrl + H in my SQL

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.