Date: 6 Oct 2005 09:42:14 -0700
Let me start off by saying that I have googled and wikki and most of
the material that comes up with Linux threads is confusing and or
dated. After tracing through a rather remarkable growth of threading
lineage from an original Linux vs Posix threads, NTPL and others, that
today pthreads API now is the most official Linux threading API. My
questions have more to do with best practices for i/o under Linux in a
threaded context, and also, perhaps exploring some evil thing.
I apologize for the length of this post in advance. If there is a
definitive and up to date FAQ for these sorts of questions, please
point me to it. If not, then, I'll assemble your answers into a
definitive thread FAQ under Linux and I'll host it on my Linux blog.
1) What's considered safe practice under Linux I/O for threading?
I'm a Windows guy and I'm used to accepting a socket connection on one
thread, then, passing it off to another thread to connect and then
process. Same for file handles. Under Linux, I understand that file
handles and socket handles are really the same thing, but, when can I
move them around?
Can I move them around across thread boundaries?
Can I share them (guarding of course with a mutex of course), across
For the case of disk I/O, and assuming I have my own mechanism to
preserve coherency, should I have one file handle shared, or should I
have multiple thread specific file handles sharing the file? Are the
file apis thread safe? My concern with multiple file handles is what
happens with file buffering. Like, I could write to FILE one thread
but that would be buffered, but the file handle for the same file on
another thread would have its own set of buffers.