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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:29:30+00:00 2026-06-05T08:29:30+00:00

I am using Boost 1.49 and MSVC10. If a boost::thread is constructed with a

  • 0

I am using Boost 1.49 and MSVC10.

If a boost::thread is constructed with a callable object1, and that object has member functions or variables I want to access from outside the context of the thread, how do I get to the callabe object?

For example, I have implemmented a simple application that spawns 5 worker threads, saved to a vector<boost::thread*> local to main(). Each of those threads is instantiated with a callable object, Gizmo which takes one char parameter in its constructor. This char is saved as a std::string member variable in the Gizmo class. Each thread will cout the saved string, then sleep for 250 ms. It continues in this loop forever, until somehow the saves string‘s value becomes “die”.

Short, Self Contained, (Hopefully) Correct, Example:

#include <cstdlib>
#include <string>
#include <memory>
#include <vector>
using namespace std;

#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

boost::mutex cout_mtx;

class Gizmo
{
public:
    Gizmo(const string& state) : state_(state) {};
    Gizmo(Gizmo&& rhs) : state_(std::move(rhs.state_)) {};
    virtual ~Gizmo()
    {
        bool b = true;
    }
    void operator()();

    string state_;
};

void Gizmo::operator()()
{
    while( state_ != "die" )
    {
        {
            boost::mutex::scoped_lock scoped_lock(cout_mtx);
            cout << state_ << flush;
        }
        boost::this_thread::sleep(boost::posix_time::milliseconds(250));
    }
}

boost::thread* start_thread(char c)
{
    Gizmo g(string(1,c));
    return new boost::thread(g);
}

int main()
{
    vector<boost::thread*> threads;

    string d=".*x%$";
    for( string::const_iterator it = d.begin(); it != d.end(); ++it )
    {
        threads.push_back(start_thread(*it));
    }

    for( auto th = threads.begin(); th != threads.end(); ++th )
        (*th)->join();
}

Now I want to make a code change in main() which will:

  1. Get the first thread in the vector
  2. Get the Gizmo object contained within that thread
  3. Set it’s state_ to “die”

How do I get the Gizmo within the thread?

for( auto th = threads.begin(); th != threads.end(); ++th )
{
    boost::this_thread::sleep(boost::posix_time::seconds(1));
    Gizmo& that_gizmo = (*th)-> ??? ;  // WHAT DO I DO?
    that_gizmo.state_ = "die";
}

(1) A callable object in this context means a class with an operator().

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

    The boost::thread does not provide the ability to get the callable object. boost::thread has a default constructor, so it does not know the callable type or function that will serve as the thread’s entry point. Also, if boost::thread did not perform type erasure, then it would no longer be possible to manage threads with different entry points in the same collection or thread pool. For example, std::vector< boost::thread< Gizmo > > could only manage Gizmo threads, while std::vector< boost::thread > can manage Gizmo threads and non-Gizmo threads.

    Instead of having to manage Gizmo and boost::thread objects in separate list, or creating a new type to pair the two together, consider associating the two via std::pair or boost::tuple. For example:

    std::vector< boost::tuple< boost::thread*, Gizmo > > threads;
    

    Also, if the threads end up having multiple types used for the entry point, and each type requires its own form of shutdown, consider performing type erasure via boost::function.
    For example:

    void shutdown_gizmo( Gizmo& );
    
    boost::function< void() > fn = boost::bind( shutdown_gizmo, boost::ref( gizmo ) );
    
    // The tuple now contains a function object, decoupling it from Gizmo objects,
    // and allowing other shutdown methods to be paired with threads.
    typedef boost::tuple< boost::thread*, boost::function< void() > > tuple_t;
    std::vector< tuple_t > threads;
    

    With any solution, just be careful about maintaining the scope of the Gizmo object, as well as maintaining a handle to the correct Gizmo object. It can be fairly easy to unintentionally obtain handles to copies of the actual Gizmo object.

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

Sidebar

Related Questions

I am using boost::tuple for my code. Suppose that I want to store an
I am Using Boost Random generator to generate Random UUID's.The Random UUID that i
I am using boost build in my project and now i want to use
Without using boost::thread and boost::bind directly, is there a way to implement the equivalent
I am using boost::thread, how can I tell if a thread is still running
I am using boost::program_options and want to pass domain-like options to my program. Such
I am using boost::asio to make a socket network. The library has a number
Using Boost.Python, is there a way to call a Python function that's been passed
When using boost::scoped_ptr to hold a reference, the destructor of the derived object is
I am using using boost libraries to parse a file. Its known that when

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.