Jump to content
  • Advertisement
Sign in to follow this  
bcome

boost::thread(*this)

This topic is 4949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to compile something like this:
class ThreadedClass
{
public:
    boost::thread *this_thread;

    ThreadedClass();
    ~ThreadedClass();

    void operator ()();
}
ThreadedClass::ThreadedClass()
{
    this_thread = new boost::thread(*this);
}
ThreadedClass::~ThreadedClass()
{
    if (this_thread) delete this_thread;
}
void ThreadedClass::operator ()()
{
    std::cout << "void ThreadedClass::operator ()()!";
    std::cout << std::endl;
}


I have this simplified class and the real class in the same project. The simplified class compiles just fine, yet the real one gives me:
c:\some\folder\someproject\somesourcefile.cpp(36) : error C2664: 'boost::thread::thread(const boost::function0<R> &)' :
cannot convert parameter 1 from 'Namespace::RealClass' to 'const boost::function0<R> &'
        with
        [
            R=void
        ]
        and
        [
            R=void
        ]
        Reason: cannot convert from 'Namespace::RealClass' to 'const boost::function0<R>'
        with
        [
            R=void
        ]
        No constructor could take the source type, or constructor overload resolution was ambiguous
What is wrong? Also I can't even do this:
typedef void (*ThreadFunction)();
ThreadFunction = *this;
I'm compiling under MSVC++ .net Toolkit 2003 with MSVC++ .net 2003 SE headers & libraries. Edit: line split for ease.

Share this post


Link to post
Share on other sites
Advertisement
You'll want to use boost::bind():
this_thread = new boost::thread(boost::bind(MyThreadFunc,this));


Quote:
typedef void (*ThreadFunction)();

ThreadFunction = *this;

Of course it won't work, this points to an instance of ThreadedClass, *not* a function. operator() doesn't make the class a function, operator() is just a normal function. Passing a class with operator() like a function will only work if the parameter is templated, ie

template<typename Func>
void dostuff(Func func)
{
func();
}

struct MyFunc
{
void operator()()
{
// do stuff
}
};

void test()
{
MyFunc myfunc;
dostuff(myfunc);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!