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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:15:44+00:00 2026-05-23T13:15:44+00:00

Consider the following code : void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber) { boost::unique_lock<boost::mutex>(mtx); subscribers.push_back(subscriber); } void ListenerImpl::notify(MsgPtr

  • 0

Consider the following code :

void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber)
{
    boost::unique_lock<boost::mutex>(mtx);
    subscribers.push_back(subscriber);
}

void ListenerImpl::notify(MsgPtr msg)
{
    boost::unique_lock<boost::mutex>(mtx);

    //notify all subscribers
    BOOST_FOREACH(boost::shared_ptr<ISubscriber> subscriber, subscribers){
        subscriber->update(msg);
    }

}

(This is an implementation of an observer pattern as described in GoF.)
The user intervention here was to protect the attach() and the notify() from simultaneous running, hence the boost::unique_lock.
The goal was to protect the subscribers container.

But it is indeed extremely hard to notice that the locks are in fact just temporaries (take a closer look, there are no names assigned for them).
So the lock on the mutex will be released immediately, when the temporary is destructed, i.e. the code is not thread safe.
I would expect in situations like this a compiler warning. Something like “Unused temporary”.

Even worse, cppcheck wouldn’t recognize this mistake either.
(cppcheck: a c/c++ code analysis tool http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=Main_Page)

Gcc issues warnings on unused variables. The temporary here is an unused variable, and definitely the result of the programmer’s inattention.
So, why there are no warnings in cases like this? Maybe it is too complicated to discover such situations?

  • 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-23T13:15:44+00:00Added an answer on May 23, 2026 at 1:15 pm

    Compiler doesn’t issue a warning, because it’s quite possible that you might be updating some static-member / global variable inside the constructor (which is valid and meaningful). e.g.:

    struct A
    {
      static int count;
      A () { count ++; }
    };
    

    Now when you simply invoke a temporary:

    A();
    

    In case if such update is not happening, compiler will not dig into the constructor of A and check if something useful is happening. It always assumes to be a valid scenario. There are many such cases can be pointed out related to temporaries.

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

Sidebar

Related Questions

Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Consider the following line of code: private void DoThis() { int i = 5;
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code: void f(byte x) {print(byte);} void f(short x) {print(short);} void f(int
Consider the following code : void populate(int *arr) { for(int j=0;j<4;++j) arr[j]=0; } int
Consider the following code: void foo() { { CSomeClass bar; // Some code here...
Consider the following C++ code: class A { public: virtual void f()=0; }; int
All, Consider the following code: void func(void) { int a; printf (%d, a); }

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.