boost.thread : Can one create a thread with an object member function?

I am very new to boost, I apologize if this has been asked before. I searched the mailing list archive but could not find an answer to my question. I was successful creating threads with functions that are not members of objects, but when I try to create threads with a pointer to an object member I get compilation errors (MS Visual Studio .NET). Examples: class objclass; typedef void (objclass::*pmf)(); // ptr to member func class objclass { public: objclass() : thread_id (0) { p=&objclass::dowork; } inline setthreadid(int x) {thread_id = x;} inline setvaltwo(std::string str) {valtwo = str;} void dowork(); pmf p; // pointer to dowork member function int getthreadid() {return thread_id;} std::string getvaltwo() {return valtwo;} private: int thread_id; std::string valtwo; }; void objclass::dowork() { } (In the main...) boost::thread_group thrds; objclass * objthread; objthread = new objclass; thrds.create_thread( (objthread->p) ); I get the following error: c:\Boost\include\boost-1_31\boost\function\function_template.hpp(427): error C2664: 'void boost::function0<R>::assign_to<Functor>(FunctionPtr,boost::detail::funct ion::function_ptr_tag)' : cannot convert parameter 2 from 'boost::detail::function::member_ptr_tag' to 'boost::detail::function::function_ptr_tag' with [ R=void, Functor=pmf, FunctionPtr=pmf ] Is there a way around this problem, or is it simply not possible to make threads from object members with boost? Thanks, Tim

Tim Laplaca wrote:
boost::thread_group thrds;
objclass * objthread;
objthread = new objclass;
thrds.create_thread( (objthread->p) );
Do this instead: boost::shared_ptr<objclass> objthread( new objclass ); thrds.create_thread( boost::bind( &objclass::dowork, objthread ) ); Even better, drop the objclass entirely and do this: void dowork( int tid, string valtwo ) { // do work } thrds.create_thread( boost::bind( dowork, my_thread_id, my_valtwo ) );

Yup! boost::thread(boost::bind(&classname::method, boost::ref(thisptr))); On Mon, 22 Nov 2004 10:50:17 -0500, Tim Laplaca <tlaplaca@voiceglo.com> wrote:
I am very new to boost, I apologize if this has been asked before. I searched the mailing list archive but could not find an answer to my question.
I was successful creating threads with functions that are not members of objects, but when I try to create threads with a pointer to an object member I get compilation errors (MS Visual Studio .NET).
Examples:
class objclass;
typedef void (objclass::*pmf)(); // ptr to member func
class objclass
{
public:
objclass() : thread_id (0) { p=&objclass::dowork; }
inline setthreadid(int x) {thread_id = x;}
inline setvaltwo(std::string str) {valtwo = str;}
void dowork();
pmf p; // pointer to dowork member function
int getthreadid() {return thread_id;}
std::string getvaltwo() {return valtwo;}
private:
int thread_id;
std::string valtwo;
};
void objclass::dowork()
{
}
(In the main...)
boost::thread_group thrds;
objclass * objthread;
objthread = new objclass;
thrds.create_thread( (objthread->p) );
I get the following error:
c:\Boost\include\boost-1_31\boost\function\function_template.hpp(427): error C2664: 'void boost::function0<R>::assign_to<Functor>(FunctionPtr,boost::detail::funct ion::function_ptr_tag)' : cannot convert parameter 2 from 'boost::detail::function::member_ptr_tag' to 'boost::detail::function::function_ptr_tag'
with
[
R=void,
Functor=pmf,
FunctionPtr=pmf
]
Is there a way around this problem, or is it simply not possible to make threads from object members with boost?
Thanks,
Tim
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Cory Nelson http://www.int64.org
participants (3)
-
Cory Nelson
-
Peter Dimov
-
Tim Laplaca