[C++] MultiThread Problem?

06/02/2014 15:54 -Unknow#1
Hi Guys ;) i've a problem with _beginthread in C++, This problem there is only when i use it in the class.
For example when i try with this :
Code:
void Class::Thread()
{
	_beginthread((void (__cdecl*)(void*))Start,0,0);
}
The problem : conversion by 'overloaded-function' to 'void (__cdecl *)(void *)';
But when i try with this :
Code:
void Class::Thread()
{
	_beginthread(this->Start,0,0);
}
The problem : &namespace::class::Thread': call of function without topics; you can use 'namespace::class::Thread' for create a pointer at member.

:( can you help me? Thanks ;)
06/02/2014 17:11 Tasiro#2
If Start is a member function, then a.Start and b->Start may only be used to call the function. For a member function pointer, you can use & Class::Start.
You cannot convert a member function pointer to a pointer to a plain function. C++ supports pointers to member functions of every class, and an efficent support requires member function pointers that contain more than one pointer. A member function pointer to a class with multiple base classes usually contains more than one pointer. If you call a member function of the second or subsequent base class with a pointer to the derived class, then the this pointer requires adjustment. Virtual base classes are scary and should be avoided.

You should use std::thread the_thread {& Class::Start, this} or std::thread thread {[this] () { /* work */ }} (or maybe auto f = std::async (...)), if you can. If you do, you must call thread::join or thread::detach for each thread (or move or swap them).
06/02/2014 17:28 snow#3
Quote:
Originally Posted by Tasiro View Post
You should use std::thread the_thread {& Class::Start, this} or std::thread thread {[this] () { /* work */ }} (or maybe auto f = std::async (...)), if you can. If you do, you must call thread::join or thread::detach for each thread (or move or swap them).
I'd rather use std::thread the_thread{ std::bind(&Class::Start, this) } to create the thread with a valid std::function object. Is there any reason why I should refrain from that and use e.g. std::thread the_thread { &Class::Start, this }? &Class::Start will probably be wrapped into a std::function object anyway, hm.
06/02/2014 17:50 Tasiro#4
Quote:
Originally Posted by snow911 View Post
I'd rather use std::thread the_thread{ std::bind(&Class::Start, this) } to create the thread with a valid std::function object. Is there any reason why I should refrain from that and use e.g. std::thread the_thread { &Class::Start, this }? &Class::Start will probably be wrapped into a std::function object anyway, hm.
You don't need to do it yourself. It's your choise.