类虚拟成员函数中的线程 [英] thread in class virtual member function

查看:137
本文介绍了类虚拟成员函数中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个具有虚拟成员函数的线程对象,可以覆盖。

i wanna write a threading object has a virtual member function ,can override.

#include <iostream>
#include <process.h>
using namespace std;

class thread
{
private:
    static void gangplank(void *ptr)
    {
        ((thread *)ptr)->run();
    }
public:
    void start()
    {
        _beginthread(&this->gangplank,0,(void *)this);
        //this->gangplank((void *)this);
    }
    virtual void run()
    {
        cout<<1;
    }
    ~thread()
    {
        _endthread();
    }
};


class d:public thread
{
public:
    void run()
    {
        cout<<2;
    }
};

int main()
{
    d a;
    a.start();

    return 0;
}




in line 18.

virtual void run()



如何制作线程对象。这个对象可以继承,成员函数可以覆盖。


how can i make a threading object.this object can be inherited and the member function can be overrode.

推荐答案

线程函数的规范不正确。使用



The specification of the thread function is not correct. Use

_beginthread (&gangplank, 0, (void *)this);





[已修改]

另外,你应该等待线程的析构函数直到新创建的workerthread结束。现在发生的是,在main中你创建一个d对象,调用它的start()函数,然后终止main。这将破坏d对象,甚至在线程有机会运行之前。然后,当最终调用线程的worker函数时,d对象已被破坏,因此其v表无效。但是这个v表需要调用虚函数。这也是您的示例在未将run函数声明为virtual的情况下运行的原因。在这种情况下,不需要v表,并且d对象已经消失并不重要。



作为一个简单的测试,放一个睡眠(1000)在_endthread调用之前。最终你需要一个更复杂的线程终止序列。



[AMENDED-2]

你的代码还有一个问题:必须从要终止的线程调用_endthread。相反,它是从主线程调用的。这甚至会让事情变得更糟。执行cout<< 2后,您的线程函数仍然会结束。所以实际上没有必要调用_endthread。如果您要将示例扩展为完整的应用程序,则需要将_endthread放入run run函数中。



[AMENDED]
In addition, you should wait in the destructor of your thread until the newly created workerthread has ended. What happens now is that in main you create a "d" object, call its start() function, and then terminate main. This will destroy the "d" object, even before the thread had a chance to run. Then, when the thread's worker function is finally called, the "d" object has already been destroyed, and hence its v-table is invalid. But this v-table is needed to call a virtual function. This is also the reason why your example runs without declaring the run function as virtual. In that case the v-table is not needed and it doesn't matter that the "d" object is already gone.

As a simple test, put a Sleep (1000) before the call of _endthread. Eventually you will need a more sophisticated thread termination sequence.

[AMENDED-2]
And there is one more problem in your code: _endthread must be called from the thread that you want to terminate. Instead it is called here from the main thread. That even makes things worse. Your thread function ends anyway after you do "cout << 2". So there is actually no need to call _endthread. If you are going to extend your example to a full application, you want to put _endthread into you run function.


这篇关于类虚拟成员函数中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆