pthread_create如何传参
PthrreadParams定义
struct PthreadParams {
CreateVTKAndPVDFile* pThis;
int myid;
/*
*其它相应的参数 省略
**/
};
pthread_create传参
PthreadParams* params=new PthreadParams[MAXTHREADS];
for (int i = 0; i < thread_nums; i++)
{
params[i].myid = i;
//myid[i] = i;
params[i].pThis = this;
pthread_create(&tid[i], nullptr, calculateCellsPerThread, (void*)¶ms[i]);
}
delete[] params;
错误的方式:
方式1:
for (int i = 0; i < thread_nums; i++)
{
PthreadParams params[MAXTHREADS]
params[i].myid = i;
//myid[i] = i;
params[i].pThis = this;
pthread_create(&tid[i], nullptr, calculateCellsPerThread, (void*)¶ms[i]);
}
方式2:
for (int i = 0; i < thread_nums; i++)
{
PthreadParams params
params.myid = i;
//myid[i] = i;
params.pThis = this;
pthread_create(&tid[i], nullptr, calculateCellsPerThread, (void*)¶ms);
}
why?