Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 05:51

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C++] MultiThread Problem?

Discussion on [C++] MultiThread Problem? within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: May 2014
Posts: 15
Received Thanks: 0
[C++] MultiThread Problem?

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
-Unknow is offline  
Old 06/02/2014, 17:11   #2
 
elite*gold: 0
Join Date: Aug 2012
Posts: 236
Received Thanks: 94
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).
Tasiro is offline  
Old 06/02/2014, 17:28   #3

 
snow's Avatar
 
elite*gold: 724
Join Date: Mar 2011
Posts: 10,479
Received Thanks: 3,318
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.
snow is offline  
Old 06/02/2014, 17:50   #4
 
elite*gold: 0
Join Date: Aug 2012
Posts: 236
Received Thanks: 94
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.
Tasiro is offline  
Reply




All times are GMT +1. The time now is 05:52.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.