模板基类的成员函数如何做为线程函数
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <pthread.h>
template<typename T>
class MyTest {
public:
MyTest(){};
~MyTest(){};
void start();
void run_loop();
};
void* main_loop(void* ctx) {
MyTest* qw = static_cast<MyTest*>(ctx);
qw->run_loop();
return NULL;
}
template<typename T>
void MyTest<T>::start() {
pthread_t tid;
pthread_create(&tid, NULL, main_loop, this);
}
int main() {
MyTest *tmp = new MyTest();
tmp->start();
return 0;
}
为代码如上,子类会继承这个模板基类,作为一个线程,请问如何实现这个线程入口?
kaede_1
2017/06/25 14:18- 模板基类
- 线程函数
- 点赞
- 收藏
- 回答