c语言面向对象:线程的派生类使用实例

前面的文章实现了父类ZThreadClass和子类MyThread1Class。
以下代码是类的使用方法和输出结果。

main.c
#include "ZThreadClass.h"
#include "MyThread1Class.h"
static void DoSomething(void * obj)
{
 ZThreadClass * th = (ZThreadClass *)obj;
 printf("\t线程 [%s] 正在运行 \n", th->threadName);
 th->ZSleep(500);
}
void ZThreadClassTest(void)
{
 ZThreadClass * th = ZThreadClassCreate();
 th->Init(th);
 th->VirtualDoSomething = DoSomething;
 th->SetThreadName(th, "父类 ZThreadClass");
 th->Start(th);
 th->ZSleep(2000);  //让线程跑一会
 th->Stop(th);
 th->ZSleep(200);
}
void MyThread1ClassTest(void)
{
 MyThread1Class * th = MyThread1ClassCreate();
 th->Init(th);
 th->threadptr->SetThreadName(th->threadptr, "子类 MyThread1Class");
 th->threadptr->Start(th->threadptr);
 th->threadptr->ZSleep(2000);  //让线程跑一会
 th->threadptr->Stop(th->threadptr);
 th->threadptr->ZSleep(200);
 printf("\n按任意键退出..... \n");
 getchar();
}
void main(void)
{
 ZThreadClassTest();
 printf("\n");
 MyThread1ClassTest();
}

输出结果:
开始线程: [父类 ZThreadClass] 。
   线程 [父类 ZThreadClass] 正在运行
   线程 [父类 ZThreadClass] 正在运行
   线程 [父类 ZThreadClass] 正在运行
   线程 [父类 ZThreadClass] 正在运行
 [父类 ZThreadClass] 线程停止。
线程 [子类 MyThread1Class] 创建资源。
开始线程: [子类 MyThread1Class] 。
   线程 [子类 MyThread1Class] 正在运行
   线程 [子类 MyThread1Class] 正在运行
   线程 [子类 MyThread1Class] 正在运行
   线程 [子类 MyThread1Class] 正在运行
   线程 [子类 MyThread1Class] 正在运行
   线程 [子类 MyThread1Class] 正在运行
   线程 [子类 MyThread1Class] 正在运行
 [子类 MyThread1Class] 线程停止。
线程 [子类 MyThread1Class] 删除资源。
按任意键退出.....


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值