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

The Archive Base Latest Questions

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

boost::mutex::scoped_lock is a handy RAII wrapper around locking a mutex. I use a similar

  • 0

boost::mutex::scoped_lock is a handy RAII wrapper around locking a mutex. I use a similar technique for something else: a RAII wrapper around asking a data interface to detach from/re-attach to a serial device.

What I can’t figure out, though, is why in the code below only my object mst — whose instantiation and destruction do have side effects — causes g++ to emit an “unused variable” warning error whereas l manages to remain silent.

Do you know? Can you tell me?

[generic@sentinel ~]$ cat test.cpp
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>

struct MyScopedThing;
struct MyWorkerObject {
    void a() { std::cout << "a"; }
    void b() { std::cout << "b"; }

    boost::shared_ptr<MyScopedThing> getScopedThing();
};

struct MyScopedThing {
    MyScopedThing(MyWorkerObject& w) : w(w) {
        w.a();
    }
    ~MyScopedThing() {
        w.b();
    }

    MyWorkerObject& w;
};

boost::shared_ptr<MyScopedThing> MyWorkerObject::getScopedThing() {
    return boost::shared_ptr<MyScopedThing>(new MyScopedThing(*this));
}

int main() {
    boost::mutex m;
    boost::mutex::scoped_lock l(m);

    MyWorkerObject w;
    const boost::shared_ptr<MyScopedThing>& mst = w.getScopedThing();
}


[generic@sentinel ~]$ g++ test.cpp -o test -lboost_thread -Wall
test.cpp: In function ‘int main()’:
test.cpp:33: warning: unused variable ‘mst’

[generic@sentinel ~]$ ./test
ab[generic@sentinel ~]$ g++ -v 2>&1 | grep version
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
  • 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-25T14:28:12+00:00Added an answer on May 25, 2026 at 2:28 pm

    Note that the question has changed since the other answers were written.

    Likely the reason g++ doesn’t warn in the current form is because mst is a reference, and constructing and destructing a reference has no side effects. It’s true that here the reference is extending the lifetime of a temporary, which has effects in its constructor and destructor, but apparently g++ doesn’t realise that makes a difference.

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

Sidebar

Related Questions

Suppose I have code something like this: #include boost/thread/mutex.hpp using boost::mutex; typedef mutex::scoped_lock lock;
class temp { boost::mutex mx; void CriticalCode() { boost::mutex::scoped_lock scoped_lock(mx); //Do Something return; }
Here's the code: ElementType& operator[] (int key) { //something like boost::mutex::scoped_lock MutexLockType lock(); if(key
In Windows environment, is Boost's scoped mutex using WinAPI's critical sections, or something else?
I've been using boost::mutex::scoped_lock in this manner: void ClassName::FunctionName() { { boost::mutex::scoped_lock scopedLock(mutex_); //do
Which is preferred boost::lock_guard or boost::mutex::scoped_lock ? I'm using Boost.Thread with the hope to
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
#include <iostream> #include <boost/thread.hpp> using std::endl; using std::cout; using namespace boost; mutex running_mutex; struct
I have a object cache class like this: #include boost/thread/mutex.hpp #include boost/unordered_map.hpp template <typename
Why can I not do this? boost::shared_ptr<QueuList> next; void QueuList::SetNextPtr(QueuList* Next) { boost::mutex mtx;

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.