问题现象:
C++中创建线程,但在线程函数中无法使用同类下的private变量与函数。
问题分析:
通过
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg);
创建线程,由于要求线程运行函数是static(上述第三个参数),导致在此函数中无法调用同类中的private变量与函数。
问题处理:
通过传参的方式,将所属类指针传入线程函数。
通过PthreadParams *arg将类指针传入线程函数:
线程函数中指针的获取:
线程函数中private变量及函数的调用:
pThis->abc = 0;
pThis->SocketABC();