我正在使用Kubuntu 12.04,gcc 4.6.3 . 如果我创建一个pthread,请使用fopen64然后fgets - 它是segfaults . 用fopen替换fopen64的相同代码 - 它成功了 . 没有创建pthread - 它成功了 . 那么为什么失败呢?这是代码:
#include <stdio.h>
#include <pthread.h>
typedef struct threadArgs
{
char* argsList;
int argc;
} threadArgs;
void
threadRun(void *pArg);
int
main(int argc, char* argv[])
{
int err = 0;
threadArgs thrArgs;
pthread_t thrd;
if (argc > 1)
{
printf("creating thread \n");
err = pthread_create (&thrd, NULL, (void *) &threadRun, (void *) &thrArgs);
printf("pthread_create returned: %d \n", err);
pthread_join(thrd, NULL);
}
else
{
printf("no thread - just calling func \n");
threadRun((void*)&thrArgs);
}
printf("Exiting main() \n");
return err;
}
void
threadRun(void *pArg)
{
printf("IN the Thread \n");
char* pStr;
FILE *pFile = NULL;
pFile = (FILE*)fopen64("test.txt","r");
//pFile = (FILE*)fopen("test.txt","r");
if (pFile==NULL)
{
printf("pFile is NULL \n");
}
else
{
printf("pFile is NOT null \n");
char line[256];
pStr = fgets(line, sizeof(line),pFile);
if (pStr)
{
printf("line retrieved: %s \n", line);
}
else
{
printf("no line retrieved \n");
}
}
printf("End of pthread run func \n");
return;
}
1 回答
pthread_create()
期望void * (*)(void *)
作为线程函数,但是你传递void (*)(void *)
.Update:
您缺少
fopen64()
的原型,因此编译器假定int
与FILE*
不同 .Update 1:
要使这个原型可用(并通过此修复您的初始问题),只需添加:
作为源文件中的第一行 .
附加编辑:确切地说:
_LARGEFILE64_SOURCE
需要在#include
之前#define
编辑stdio.h
Update 2:
遵循我曾经使suxxer工作的来源(
main.c
):Build 者:
(没有其他错误或警告)
环境:
输出(使用
main.c
的源而不\n
s为test.txt
):