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:
- Get the first
thread
in thevector
- Get the
Gizmo
object contained within thatthread
- 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()
.