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 8313053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:22:14+00:00 2026-06-08T20:22:14+00:00

I need to implement (in C++) a thread safe container in such a way

  • 0

I need to implement (in C++) a thread safe container in such a way that only one thread is ever able to add or remove items from the container. I have done this kind of thing before by sharing a mutex between threads. This leads to a lot of mutex objects being littered throughout my code and makes things very messy and hard to maintain.

I was wondering if there is a neater and more object oriented way to do this. I thought of the following simple class wrapper around the container (semi-pseudo C++ code)

 class LockedList {
    private:
        std::list<MyClass> m_List;

    public:
        MutexObject Mutex;
 };

so that locking could be done in the following way

 LockedList lockableList;     //create instance
 lockableList.Mutex.Lock();    // Lock object

 ... // search and add or remove items

 lockableList.Mutex.Unlock();   // Unlock object

So my question really is to ask if this is a good approach from a design perspective? I know that allowing public access to members is frowned upon from a design perspective, does the above design have any serious flaws in it. If so is there a better way to implement thread safe container objects?

I have read a lot of books on design and C++ in general but there really does seem to be a shortage of literature regarding multithreaded programming and multithreaded software design.

If the above is a poor approach to solving the problem I have could anyone suggest a way to improve it, or point me towards some information that explains good ways to design classes to be thread safe??? Many thanks.

  • 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-06-08T20:22:15+00:00Added an answer on June 8, 2026 at 8:22 pm

    I would rather design a resourece owner that locks a mutex and returns an object that can be used by the thread. Once the thread has finished with it and stops using the object the resource is automatically returned to its owner and the lock released.

    template<typename Resource>
    class ResourceOwner
    {
          Lock         lock; 
          Resource     resource;
    
          public:
             ResourceHolder<Resource>  getExclusiveAccess()
             {
                  // Let the ResourceHolder lock and unlock the lock
                  // So while a thread holds a copy of this object only it
                  // can access the resource. Once the thread releases all
                  // copies then the lock is released allowing another
                  // thread to call getExclusiveAccess().
                  //
                  // Make it behave like a form of smart pointer
                  //    1) So you can pass it around.
                  //    2) So all properties of the resource are provided via ->
                  //    3) So the lock is automatically released when the thread
                  //       releases the object.
    
                  return ResourceHolder<Resource>(lock, resource);
             }
    };
    

    The resource holder (not thought hard so this can be improved)

    template<typename Resource>
    class ResourceHolder<
    {
        // Use a shared_ptr to hold the scopped lock
        // When first created will lock the lock. When the shared_ptr
        // destroyes the scopped lock (after all copies are gone)
        // this will unlock the lock thus allowding other to use
        // getExclusiveAccess() on the owner
        std::shared_ptr<scopped_lock>    locker;
        Resource&                        resource;   // local reference on the resource.
    
        public:
            ResourceHolder(Lock& lock, Resource& r)
                : locker(new scopped_lock(lock))
                , resource(r)
            {}
    
            // Access to the resource via the -> operator
            // Thus allowing you to use all normal functionality of 
            // the resource.
            Resource* operator->() {return &resource;}
    };
    

    Now a lockable list is:

    ResourceOwner<list<int>>  lockedList;
    
    void threadedCode()
    {
        ResourceHolder<list<int>>  list = lockedList.getExclusiveAccess();
    
        list->push_back(1);
    }
    // When list goes out of scope here. 
    // It is destroyed and the the member locker will unlock `lock`
    // in its destructor thus allowing the next thread to call getExclusiveAccess()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was recently looking for a way to implement a doubly buffered thread-safe cache
Is there a good way to implement an execution policy that determines which Thread
I need to implement a notification mechanism for a system that has one manager
I need to implement simple thread-safe memory management preventing fragmentation. I've read some articles
I need to implement a thread pool in Java (java.util.concurrent) whose number of threads
i need to implement the email signature with image.As of now we only support
I need to implement a one-to-one videoconferencing solution server-based, runnable by browser, free (or
i need to implement a messaging scenario that consumes messages from throusands of destinations
Hi guys I need to implement a way to timeout my objects without using
Is the Go map type thread safe? I have a program that has many

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.