c ++,pthread和静态回调.& quot; this& quot;返回指向派生类的基类inctead的指针 [英] c++ , pthread and static callbacks. "this" returns a pointer to the base class inctead of the derived one
问题描述
我正在使用此 cpp线程类.我将其用作线程的基类.请注意,在我的情况下,Thread :: main()是一个虚函数(与链接不同).所以我基本上使用:
I am using as basis this cpp thread class. This I use as a base class for threads. Note that in my case Thread::main() is a virtual function (unlike in the link). So I basically use:
class Thread {
public:
virtual void main()
{
cout << "This should not be run once derived and redefined." << endl;
}
void run()
{
pthread_create(&thread, 0, &callback, this);
}
pthread_t thread;
}; // class Thread
void* callback(void* obj)
{
static_cast<Thread*>(obj)->main();
return(0);
} // callback
然后,我创建一个派生类,并重新定义 myThreadedClass :: main()
成员,以实际做一些有意义的事情.
Then I create a derived class and re-define the myThreadedClass::main()
member to actually do something meaningful.
最后,我从其他类或主函数调用中实例化myThreadedClass对象,如下所示:
Finally, I instantiate the myThreadedClass object from other classes or my main function call as follows:
main(int argc, char** argv){
myThreadedClass thr;
thr.run();
//do other stuff
}
这很好用;回调函数获取指向派生类实例的指针,因此,将执行 myThreadedClass :: main()
.
This works fine; The callback function gets a pointer to the derived class instantiation, so the myThreadedClass::main()
gets executed.
但是,我现在尝试创建另一个派生类 class otherThreadClass:公共线程
.再次,我重新定义了 otherThreadClass :: main()
,但是现在我在派生类中有一个成员函数(与以前不同),该成员函数调用Thread :: run().
However, I now try to create a different derived class class otherThreadClass : public Thread
. Again I re-define my otherThreadClass::main()
, but now I have a member function in the derived class which (unlike before) calls Thread::run().
class otherThreadClass : public Thread{
public:
writeToDiskAsync(string& str){
prepareString(str);
//spawn a thread to carry the write without blocking execution
run();
}
};
在这种情况下,我的主要功能是
in this case from my main function I do
main(int argc, char** argv){
otherThreadClass thr;
thr.writeToDiskAsync(aString);
//do other stuff
}
这种情况下的问题是,回调函数获取指向Thread类的指针,并且Thread :: main()最终被执行,而不是otherThreadClass :: main().
The problem in this case is that the callback function gets a pointer to the Thread class and the Thread::main() ends up being executed instead of the otherThreadClass::main().
我尝试在实例化过程中将指针传递给实例化的myThreadedClass对象(使用初始化列表和对Thread :: run(void * instance)的更改调用),如下所示:
I tried passing a pointer to the instantiated myThreadedClass object during instantiation (using initialisation lists and an altered call to Thread::run(void* instance)) as follows
//in main function
otherThreadClass thr(&thr);
//in class
otherThreadClass::otherThreadClass(otherThreadClass* ptr):instancePtr(ptr)
{}
otherThreadClass::writeToDiskAsync(string& str)
{
//do stuff
run(instancePtr);
}
//and finally
Thread::run(void* parentObj)
{
pthread_create(&thread, 0, &callback, parentObj);
}
但是它不起作用.而且我认为这可能不是一个好方法.那么我该怎么做才能让回调函数将指针指向派生类实例而不是基类呢?
but it does not work. And I think this is probably not a nice way to do it anyway. So what can I do to let the callback function get apointer to the derived class instance instead of the base class ?
谢谢
推荐答案
如果您尝试使用基类ptr调用函数,则只要函数不是虚拟的,每次都会调用基类版本.因此,解决您的问题的更简单方法是将main设置为如下所示:
If you will try to call a function using base class ptr , everytime base class version gets called as long as function is not virtual . so simpler solution to your problem would be to make main virtual as below :
#include <iostream>
#include<pthread.h>
#include<unistd.h?
using namespace std;
void* callback(void* obj);
class Thread {
public:
virtual int main()
{
cout << "Hi there Base class" << endl;
return(0);
}
void run()
{
pthread_create(&thread, 0, &callback, this);
}
pthread_t thread;
};
class otherThreadClass : public Thread{
public:
virtual int main()
{
cout << "Hi there other class" << endl;
return(0);
}
void writeToDiskAsync(string str){
//spawn a thread to carry the write without blocking execution
run();
}
};
class Thread;
void* callback(void* obj)
{
static_cast<Thread*>(obj)->main();
return(0);
} // callback
int main() {
// your code goes here
otherThreadClass thr;
thr.writeToDiskAsync(string("aS"));
sleep(10);//it is neccessary as main thread can exit before .
return 0;
}
output : Hi there other class
如果main不是虚拟的,它将在您通过基类ptr调用时始终调用main的基类版本(将发生静态绑定)
Whereas if main is not virtual , it will always call base class version of main as you are calling through a base class ptr (static binding will happen )
这篇关于c ++,pthread和静态回调.& quot; this& quot;返回指向派生类的基类inctead的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!