Heres my code:
void *PrintLine(void *line)
{
printf("Line: #%s\n", (char *)line);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
char line[80];
while(fgets(line,sizeof(line),fp))
{
pthread_create(&threads[rt], NULL, PrintLine, (void*)line);
}
fclose(fp);
}
Please dont tell me that running a thread only to print a file line doesn’t make sense, I removed a lot of code so its easier to read and understand my problem.
As you would guess this code doesn’t work, what should I do to be able to print/use “line” inside the thread?
You’re passing a pointer to
lineto the newly created thread, when your thread gets around to useline, perhapse fgets have written something else to it. Or perhaps it’s in the middle of writing something when your thread accesses it.You can pass a copy of the line you’ve read, remember to
free()it when you’re done with it inside you thread.