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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:18:06+00:00 2026-05-29T08:18:06+00:00

I am working on a set that is frequently read but rarely written. class

  • 0

I am working on a set that is frequently read but rarely written.

class A {
  boost::shared_ptr<std::set<int> > _mySet;
public:
  void add(int v) {
    boost::shared_ptr<std::set<int> > tmpSet(new std::set<int>(*_mySet));
    tmpSet->insert(v);  // insert to tmpSet
    _mySet = tmpSet;    // swap _mySet
  }
  void check(int v) {
    boost::shared_ptr<std::set<int> > theSet = _mySet;
    if (theSet->find(v) != theSet->end()) {
      // do something irrelevant
    }
  }
};

In the class, add() is only called by one thread and check() is called by many threads. check() does not care whether _mySet is the latest or not. Is the class thread-safe? Is it possible that the thread executing check() would observe swap _mySet happening before insert to tmpSet?

  • 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-29T08:18:06+00:00Added an answer on May 29, 2026 at 8:18 am

    This is an interesting use of shared_ptr to implement thread safety.
    Whether it is OK depends on the thread-safety guarantees of
    boost::shared_ptr. In particular, does it establish some sort of
    fence or membar, so that you are guaranteed that all of the writes in
    the constructor and insert functions of set occur before any
    modification of the pointer value becomes visible.

    I can find no thread safety guarantees whatsoever in the Boost
    documentation of smart pointers. This surprizes me, as I was sure that
    there was some. But a quick look at the sources for 1.47.0 show none,
    and that any use of boost::shared_ptr in a threaded environment will
    fail. (Could someone please point me to what I’m missing. I can’t
    believe that boost::shared_ptr has ignored threading.)

    Anyway, there are three possibilities: you can’t use the shared pointer
    in a threaded environment (which seems to be the case), the shared
    pointer ensures its own internal consistency in a threaded environment,
    but doesn’t establish ordering with regards to other objects, or the
    shared pointer establishes full ordering. Only in the last case will
    your code be safe as is. In the first case, you’ll need some form of
    lock around everything, and in the second, you’ll need some sort of
    fences or membar to ensure that the necessary writes are actually done
    before publishing the new version, and that they will be seen before
    trying to read it.

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

Sidebar

Related Questions

I have a large data set that I'm working with in excel. About 1000+
I am working with a set of T4 script that generate partial classes for
Working with an undisclosed API, I found a function that can set the number
I'm working on creating a Rails app that allows users to set availability Sunday
I have a malfunctioning set of jquery datepickers that are only not working since
I have a working program in C++ that generates data for a Mandelbrot Set.
Hello I'm working on a problem that requires me to change an set array
To set the scene, I'm currently working on an XML adapter for Android that
When working with powershell cmdlets, I often find that I need to set a
I am working with a data set that has a column with values that

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.