Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safe singleton #5018

Merged
merged 6 commits into from Aug 22, 2014
Merged

Thread safe singleton #5018

merged 6 commits into from Aug 22, 2014

Conversation

ianna
Copy link
Contributor

@ianna ianna commented Aug 21, 2014

  • Thread safe Singleton
  • DDAxis map initialization at construction

@ianna
Copy link
Contributor Author

ianna commented Aug 21, 2014

@Dr15Jones and @ktf - Please, review the changes to Singleton class. Comments are welcome.

It seems to me some of the singletons (as DDAxes) can be simplified to a static const map, for example. DDRoot friendship will be reviewed next. Thanks, Yana

static std::unique_ptr<value_type> m_instance;
static std::once_flag m_onceFlag;
Singleton(void);
Singleton(const Singleton&);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To stop the compiler from creating this, you can now say
Singleton(const Singleton&) = delete;

@Dr15Jones
Copy link
Contributor

@ianna Your use of std::call_once is correct, however in this case it was not needed. C++11 guarantees that the construction of function level statics is thread safe. That is, if another thread comes into the function while another thread is initializing the static, the second thread is guaranteed to wait until the static has been initialized.

@cmsbuild
Copy link
Contributor

A new Pull Request was created by @ianna for CMSSW_7_2_X.

Thread safe singleton

It involves the following packages:

DetectorDescription/Base
DetectorDescription/Core

@civanch, @Dr15Jones, @ianna, @mdhildreth, @cmsbuild, @nclopezo, @Degano, @ktf can you please review it and eventually sign? Thanks.
You can sign-off by replying to this message having '+1' in the first line of your reply.
You can reject by replying to this message having '-1' in the first line of your reply.

static std::unique_ptr<value_type> m_instance;
static std::once_flag m_onceFlag;
Singleton(void);
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton &);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you also want to get rid of the operator=

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, thanks

@ianna
Copy link
Contributor Author

ianna commented Aug 21, 2014

@Dr15Jones - Thanks, I've fixed the copy constructor and can remove std::call_once - I was not sure if all the compilers we use are fully C++11 compliant.

@cmsbuild
Copy link
Contributor

Pull request #5018 was updated. @civanch, @Dr15Jones, @ianna, @mdhildreth, @cmsbuild, @nclopezo, @Degano, @ktf can you please check and sign again.

@cmsbuild
Copy link
Contributor

Pull request #5018 was updated. @civanch, @Dr15Jones, @ianna, @mdhildreth, @cmsbuild, @nclopezo, @Degano, @ktf can you please check and sign again.

template <class I>
I& Singleton<I>::instance() {
static I value;
return value;
std::call_once(m_onceFlag,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not needed, please revert back to the previous implementation.

@ianna ianna closed this Aug 21, 2014
@cmsbuild
Copy link
Contributor

@cmsbuild
Copy link
Contributor

@ianna ianna reopened this Aug 22, 2014
@cmsbuild
Copy link
Contributor

Pull request #5018 was updated. @civanch, @Dr15Jones, @ianna, @mdhildreth, @cmsbuild, @nclopezo, @Degano, @ktf can you please check and sign again.

@ianna
Copy link
Contributor Author

ianna commented Aug 22, 2014

@cmsbuild
Copy link
Contributor

@cmsbuild
Copy link
Contributor

@ianna
Copy link
Contributor Author

ianna commented Aug 22, 2014

+1

@cmsbuild
Copy link
Contributor

This pull request is fully signed and it will be integrated in one of the next CMSSW_7_2_X IBs unless changes (tests are also fine).

ktf added a commit that referenced this pull request Aug 22, 2014
@ktf ktf merged commit e29779b into cms-sw:CMSSW_7_2_X Aug 22, 2014
@Dr15Jones
Copy link
Contributor

@ianna The static analyzer is complaining that the static is non const. That means multiple threads can simultaneously modify the statics memory which leads to a race condition. Only if the class 'I' is specially written such that all its member functions and member data are thread safe would that static be thread safe. So it isn't the initialization that the static analyzer is complaining about, it is the subsequent use of the object instance.

@ianna ianna deleted the thread-safe-singleton branch October 26, 2015 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants