结构值在pthread_create上更改

时间:2015-04-06 14:35:15

标签: c struct pthreads

我很难理解为什么在调用pthread_create时我的struct数据被更改了。如果来自func1的代码放在main中,它工作正常,这让我觉得pthread_create函数中有tid数组发生了一些事情。感谢。

STRUCT:

typedef struct {
  char * fileName;
  char outputPath[1000];
  char outputFileName[1000];
  int fileSize;
} FileInfo;

主:

...
pthread_t tid[100];
int * tidSize = (int *)malloc(sizeof(int));
*tidSize=0;
func1(tid, tidSize);
int i;

for(i=0;i<*tidSize;i++){
    pthread_join(tid[i], NULL;
}

func1的:

void folderFound(pthread_t tid[], int * tidSize){
...
DIR * dir = opendir(".");
struct dirent * file1;
while(file1 = readdir( dir ) ) != NULL):
  (*tidSize)++;
  FileInfo * fileInfoThread = (FileInfo *)malloc(sizeof(FileInfo));
  fileInfoThread->fileName = file1->d_name;
  struct stat stInput;
  stat(file1->d_name, &stInput);
  fileInfoThread->fileSize=stInput.st_size;
  strcat(fileInfoThread->outputFileName,file1->d_name);
  strcat(fileInfoThread->outputPath,fileInfoThread->outputFileName);
  //    ==============================
  printf("%s\n",fileInfoThread->outputPath); 
  printf("%s\n",fileInfoThread->fileName);
  printf("%d\n",fileInfoThread->fileSize);
  //    ==============================  THESE ALL PRINT CORRECTLY
  int rc = pthread_create(&tid[*tidSize-1], NULL, fileThread, fileInfoThread);

fileThread:

void *fileThread(void * arg){
 FileInfo * file = (FileInfo *)(arg);
 // =======================================
  printf("%s\n",file->outputPath);
  printf("%s\n",file->fileName);
  printf("%d\n",file->fileSize);
 // ======================================= DATA IS CORRUPT/MODIFIED HERE
  free(arg);
  pthread_exit(NULL);
 }

0 个答案:

没有答案