c++ - 类虚拟成员函数中的线程

标签 c++ multithreading class overriding virtual-functions

我想制作一个线程对象,成员函数“run”可以被覆盖。当我添加“虚拟”一词时,它会失败。有人可以帮我吗 - 我怎样才能制作一个线程对象。对象可以继承,成员函数可以重写。

#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;
}

错误信息:

"text.exe Has stopped working - Windows is checking for a solution to the problem"

没有编译错误

最佳答案

我不知道这是不是你的问题,因为你只是这么说 失败了,没有说如何,但你不等待线程 在 main 中完成,因此您可能正在破坏线程对象 在线程开始运行之前。

关于c++ - 类虚拟成员函数中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17443435/

相关文章:

c++ - 错误: 'ALIGN' undeclared (first use in this function) with ALIGN defined into macro

c++ - 表示表达式计算c++(多态设计)

java - “找不到或加载主类”是什么意思?

c - 为什么在c中将指针转换为双指针?

c++ - 通用函数返回函数指针的特化类

c++ - 将 char[] 转换为 char*

java - java中单CPU的线程调度程序?

c++ - Win32 : How do I enumerate all the threads belonging to a process in C++?

java - 如何让 TestNG 在关闭之前等待我的测试完成

Java 程序在调用特定类的任何方法时卡住