PDA

View Full Version : QFile and thread-safety



Raistlin
31st January 2008, 11:00
I know (almost) all methods in QFile are reentrant, but what about thread-safety?

- Can two threads access the hard disk at the same time, so in other words, is disk access serialized internally?
- Can two threads access the same file at the same time? I suppose this is not an issue if the first point is not an issue, since serialized hard disk access would mean there are no potential conflicts here.

wysota
31st January 2008, 13:45
- Can two threads access the hard disk at the same time, so in other words, is disk access serialized internally?
- Can two threads access the same file at the same time? I suppose this is not an issue if the first point is not an issue, since serialized hard disk access would mean there are no potential conflicts here.

This all depends on the operating system. QFile simply forwards its tasks to native file API.

As a general rule you shouldn't write to the same file from more than one context at once (be it threads or processes), because the order of calls can differ and filling the file buffer might even cause different datagrams to be mixed together.

Raistlin
31st January 2008, 15:42
Luckily, I can only read the same file from different threads, each thread writes different files to disk. So I guess in this context I can not expect any real difficulties.