7

I have a multithreaded application that is opening and reading the same file (not writing). I am opening a different file descriptor for each thread (but they all point to the same file). Each thread then reads the file and may close it and open it again if EOF is reached. Is this ok? If I perform fclose() on a file descriptor does it affect the other file descritptors that point to the same file?

4

3 回答 3

4

对于 Linux 系统,您不需要多个文件描述符来执行此操作。您可以共享单个文件描述符并使用pread原子地执行查找/读取操作,而无需修改文件描述符。

于 2013-07-10T14:41:06.020 回答
3

没关系。您可以在任何时候打开您想要相同的文件,并且每个文件描述符将彼此独立。

于 2010-04-18T21:05:32.850 回答
0

如果每个线程都有自己的文件句柄,那应该可以正常工作。既然你提到了使用 of fclose(),这表明你也在fopen()每个线程中使用,并且每个线程只影响它自己的FILE *变量。

有问题吗?

于 2010-04-18T21:06:09.503 回答