65,208
社区成员
发帖
与我相关
我的任务
分享
#include <Windows.h>
#include <iostream>
class A
{
public:
void start()
{
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)A::THREAD,this,NULL,NULL);
}
void stops()
{
if(hThread)
CloseHandle(hThread);
hThread = NULL;
}
bool isRun(){return hThread != NULL;}
virtual bool onRun() = 0;
private:
static DWORD CALLBACK THREAD(A* ta)
{
while(ta->isRun() && ta->onRun());
ta->stops();
return 0;
}
private:
HANDLE hThread;
};
class B : public A
{
protected:
bool onRun()
{
std::cout<<"B Run"<<std::endl;
return true;
}
};
class C : public A
{
protected:
bool onRun()
{
std::cout<<"C Run"<<std::endl;
return true;
}
};
int main()
{
A* b = new B();
A* c = new C();
b->start();
c->start();
Sleep(5000);
c->stops(); //为什么这里暂停的时候B也一样停止了
return 0;
}