Hello,
I have taken interest in using Boost Threads in my project. I have a strange issue that I am trying to debug.
I
spawn N boost::thread instances because I know exactly how many I
need. My program crashes after a time on a boost::bad_lexical_cast
exception. I have placed a wrapper around boost::lexical_cast which
uses a boost::recursive_mutex and boost::lock_guard combination to
prevent multiple threads from calling boost::lexical_cast at the same
time.
Still, my project invariably chokes on boost::bad_lexical_cast
being thrown when I can verify the cast should work i.e. everything
works as expected in my single threaded implementation.
My real
cause for alarm is this....when I act to debug my project I always
discover there are more active boost::thread instances than I have
explicitly created. I only create boost::thread instances in one
location.
I have perused the documentation but as of yet cannot find an
explanation for this behavior. My assumption is that unless I
explicitly instantiate a boost::thread it should not exist. However, I
want to ask if this is indeed the case? If not, how so and what is the
rationale behind this behavior?
Here is what my code more or less looks like - this is all I do
with boost::threads currently - how are more than N boost::threads
spawning where N is the number of elements in an arbitrary stl
container which itself is not allowed to change in size?
typedef boost::shared_ptr<boost::
thread> THREADHANDLE;
typedef std::vector<THREADHANDLE> THREADHANDLECONTAINER;
THREADHANDLECONTAINER workerThreads;
workerThreads.reserve(some_container.size());
...
workerThreads.push_back(
THREADHANDLE(new THREADHANDLE::element_type(
Callable,
arg1,
boost::ref(arg2));
...
BOOST_FOREACH(const THREADHANDLE &workerThread,workerThreads)
{
workerThread->join();
}//end loop
------------------------------------------
Here
is my attempt at using boost::lexical_cast in a generic thread safe
manner. I am expecting exactly one thread my call boost::lexical_cast
at a time with this wrapper yet my application blows up solely on
boost::bad_lexical_cast whenever I introduce Boost Threads.
Am I missing the elephant in the room somehow?
//LexicalCaster.h
#include <boost/thread.hpp>
struct LexicalCaster
{
template<typename TargetType,typename SourceType>
inline static TargetType LexicalCast(const SourceType &source);
private:
static boost::recursive_mutex& GetMutex();
static int& GetCount();
};
//LexicalCaster.cpp
#include "LexicalCast.h"
boost::recursive_mutex _mtx;
int count(0); //here for debugging purposes
boost::recursive_mutex& LexicalCaster::GetMutex()
{
return _mtx;
}
int& LexicalCaster::GetCount()
{
return count;
}
//LexicalCaster.inl
#include <boost/lexical_cast.hpp>
template<typename TargetType,typename SourceType>
TargetType LexicalCaster::LexicalCast(const SourceType &source)
{
boost::lock_guard<boost::recursive_mutex> guardian(LexicalCaster::GetMutex());
std::cerr<<"Count: "<<LexicalCaster::GetCount()++<<std::endl; //the count will display sequentially if only
//one thread of execution has access at a time
return boost::lexical_cast<TargetType>(source);