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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:28:15+00:00 2026-05-11T16:28:15+00:00

I’m new to C++ and am writing a multi-threaded app whereby different writers will

  • 0

I’m new to C++ and am writing a multi-threaded app whereby different writers will be pushing objects onto a stack and readers pulling them off the stack (or at least pushing the pointer to an object)..

Are there any structures built-into C++ which can handle this without adding locking code etc.? If not, what about the Boost libraries?

EDIT:

Hi. Thanks for the initial great answers. I guess one reason I thought this could be built-in was that I was thinking purely in x86 space and thought that a PUSH/POP of pointers should be an atomic action at the instruction level.

I’m not sure if my initial hunch is true or not, but I guess this would not necessarily be true across all platforms. Though if running on x86, do you get atomic PUSHes and POPs to the stack and if so, does this essentially make it lock-free?

  • 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-11T16:28:15+00:00Added an answer on May 11, 2026 at 4:28 pm

    Yep: Boost.Thread is great, and should fit your needs very well. (These days, many people say that you could almost count Boost as built-in functionality.)

    There is still no class that you could use out-of-the-box, but once you have the synchronization primitives at hand, it really is quite simple to implement your own thread-safe wrapper around, for example, std::stack. It could look something like this (not implementing every method…):

    template <typename T> class MyThreadSafeStack {
      public:
        void push(const T& item) {
          boost::mutex::scoped_lock lock(m_mutex);
          m_stack.push(item);
        }
        void pop() {
          boost::mutex::scoped_lock lock(m_mutex);
          m_stack.pop();
        }
        T top() const { // note that we shouldn't return a reference,
                        // because another thread might pop() this
                        // object in the meanwhile
          boost::mutex::scoped_lock lock(m_mutex);
          return m_stack.top();
        }
    
      private:
        mutable boost::mutex m_mutex;
        std::stack<T> m_stack;
    }    
    

    If you are new to C++, please learn about RAII. Relevant to this case, Boost.Thread has the “scoped lock” classes to make it difficult to shoot yourself in the leg by forgetting to release a lock.

    If you ever find yourself writing code like this:

    void doStuff() {
      myLock.lock();
      if (!condition) {
        reportError();
        myLock.unlock();
        return;
      }
      try {
        doStuffThatMayThrow();
      }
      catch (std::exception& e) {
        myLock.unlock();
        throw e;
      }
      doMoreStuff();
      myLock.unlock();
    }
    

    , then you should just say no, and go RAII instead (syntax not directly from Boost):

    void doStuff() {
      scoped_lock lock;
      if (!condition) {
        reportError();
        return;
      }
      doStuffThatMayThrow();
      doMoreStuff();
    }
    

    The point is that when the scoped_lock object goes out of scope, its destructor releases the resource — in this case, the lock. This will always happen, no matter whether you exit the scope by throwing an exception, or by executing the odd return statement that your colleague sneakily added in the middle of your function, or simply by reaching the end of the function.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I've got a string that has curly quotes in it. I'd like to replace
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.