pthread_create()创建新线程,重写基类虚函数
问题描述在core/include/base_thread.hpp第 58 行附近定义了两个虚函数onInit和onRun以及在server_module/src/global_setup.cpp第 297 行调用了CSchedulerCWorkerThread::launch()的地方想确认CWorkerThread是否重写了这两个虚函数onInit和onRun。分析过程base_thread头文件中基类声明了oninit和onrun函数在worker.cpp中实现了这两个函数首先梳理相关文件与类的继承关系而不是只根据调用点做判断读取base_thread.hpp搜索CWorkerThread、onInit、onRun、initProc等关键字。读取global_setup.cpp实际位于server_module/src/下而非include/下。继续读取worker.hpp、worker.cpp、thread_impl.cpp确认各函数的声明与实现位置。搜索CScheduler模板的定义读取scheduler.hpp与scheduler.inl确认线程启动流程。结论onInit()和onRun()均有重写initProc()没有也不需要重写。具体说明如下函数基类中的性质CWorkerThread 是否重写onInit()virtual可被派生类覆盖有重写onRun()virtual可被派生类覆盖有重写initProc()普通成员函数非virtual不能以虚函数方式重写重写的位置core/include/worker.hpp中声明了CWorkerThread::onInit()和CWorkerThread::onRun()core/src/worker.cpp中分别给出了它们的具体实现实际调用流程CSchedulerCWorkerThread::launch(*WORKER_THD()) └─ pthread_create(...)传入 CWorkerThread 对象 └─ _threadEntryProc(CBaseThread* thd) ├─ thd-initProc() // 调用基类 CBaseThread::initProc() │ └─ onInit() // 虚函数派发 → CWorkerThread::onInit() └─ while (!exitFlag) └─ thd-onRun() // 虚函数派发 → CWorkerThread::onRun()因此global_setup.cpp中启动的CWorkerThread工作线程初始化入口虽然走的是基类的initProc()但其内部调用的onInit()以及循环中的onRun()都会通过虚函数机制派发到CWorkerThread自己重写的版本。