C++ c++;向pthreads传递信息的问题

C++ c++;向pthreads传递信息的问题,c++,pthreads,corruption,C++,Pthreads,Corruption,我遇到的问题是showData()中的printf debug语句将给我提供无意义的数字。i、 e:螺纹id为-1781505888。 如果在设置thread_id、startIndex和stopIndex的值之后立即在createThreads()中插入printf语句,那么这些值将正确打印。当我将threadData作为参数传递给pthread_create或从showData()中的threadArg读取数据时,数据不知何故被破坏。此外,可以假设N和k的值为整数,N/k的余数为0。感谢所有

我遇到的问题是showData()中的printf debug语句将给我提供无意义的数字。i、 e:螺纹id为-1781505888。 如果在设置thread_id、startIndex和stopIndex的值之后立即在createThreads()中插入printf语句,那么这些值将正确打印。当我将threadData作为参数传递给pthread_create或从showData()中的threadArg读取数据时,数据不知何故被破坏。此外,可以假设N和k的值为整数,N/k的余数为0。感谢所有的帮助

编辑:如果它提供了任何额外的信息-当我在同一个输入上运行时,每次运行它都会得到不同的输出。有时是无意义的数字,有时所有的值都打印为0,有时它会区分错误

void createThreads(int k){
struct threadData threadData;
int numThreads = k;
int i = 0;
int err = 0;

pthread_t *threads = static_cast<pthread_t*>(malloc(sizeof(pthread_t) * numThreads));
for(i = 0;i<numThreads;i++){
    struct threadData threadData;
    threadData.thread_id = i;
    threadData.startIndex = ((N/k)*i);
    if(i == numThreads -1){
        threadData.stopIndex = ((N/k)*(i+1))-1;
    }
    else{
        threadData.stopIndex = ((N/k)*(i+1));
    }



    err = pthread_create(&threads[i], NULL, showData, (void *)&threadData); 


    if(err != 0){
        printf("error creating thread\n");
    }
}
}

void *showData(void *threadArg){
    struct threadData *threadData;
    threadData = (struct threadData *) threadArg;

    printf("thread id : %d\n", threadData->thread_id);
    printf("start: %d\n", threadData->startIndex);
    printf("stop : %d\n", threadData->stopIndex);
}
void createThreads(int k){
struct threadData threadData;
int numThreads=k;
int i=0;
int err=0;
pthread_t*threads=static_cast(malloc(sizeof(pthread_t)*numThreads));
对于(i=0;i读取id);
printf(“开始:%d\n”,threadData->startIndex);
printf(“停止:%d\n”,线程数据->停止索引);
}

线程数据
是for循环的本地数据。。。它在每次迭代时都超出范围,因此指向它的指针在
showData()
例程中无效。您可以动态地分配它,并在
showData
的末尾删除它

您可能也想将
线程
数据返回给
createThreads
”调用程序,这样它就可以
加入线程,等待
showData
的“工作”完成

例如:

...
for(i = 0; i < numThreads; ++i)
{
    struct threadData* threadData = new struct threadData;
    threadData->thread_id = i;
    threadData->startIndex = ((N/k)*i);
    if(i == numThreads -1){
        threadData->stopIndex = ((N/k)*(i+1))-1;
    }
    else{
        threadData->stopIndex = ((N/k)*(i+1));
    }

    err = pthread_create(&threads[i], NULL, showData, (void*)threadData); 

    if(err != 0){
        printf("error creating thread\n");
        exit(1); // probably not worth trying to continue...
    }
    return threads;
}

void *showData(void *threadArg){
    struct threadData* threadData = (struct threadData*)threadArg;

    printf("thread id : %d\n", threadData->thread_id);
    printf("start: %d\n", threadData->startIndex);
    printf("stop : %d\n", threadData->stopIndex);

    delete threadData;
}
。。。
对于(i=0;ithread\u id=i;
threadData->startIndex=((N/k)*i);
if(i==numThreads-1){
threadData->stopIndex=((N/k)*(i+1))-1;
}
否则{
threadData->stopIndex=((N/k)*(i+1));
}
err=pthread_create(&threads[i],NULL,showData,(void*)threadData);
如果(错误!=0){
printf(“创建线程时出错\n”);
退出(1);//可能不值得继续尝试。。。
}
返回线程;
}
void*showData(void*threadArg){
struct threadData*threadData=(struct threadData*)threadArg;
printf(“线程id:%d\n”,threadData->thread\u id);
printf(“开始:%d\n”,threadData->startIndex);
printf(“停止:%d\n”,线程数据->停止索引);
删除线程数据;
}

@jeremy:添加了一些说明性代码。我建议您为
struct
s和
class
s选择不同于您的变量的标识符。。。e、 g.
ThreadData
。。。这样,您就不必在代码中浪费时间,例如
struct threadData threadData
可以说
ThreadData ThreadData-当您引用类与类的实例时,可以清楚地看到这一点。在遵循您的命令之后,我仍然从printf语句中收到相当随机的数字suggestions@jeremy你能在你的问题后面加上完整的代码吗?如果没有这些,我就不能告诉你了。@jeremy我实际上在你发布代码之前就开始为你输入检查信息,然后认为这太投机了-它是
(void*)和threadData
-你忘了删除
&
。谢谢你的帮助和时间。现在工作正常吗