In the past week I had an interesting discussion in a German Usenet group. The person who started the thread, actually had a question about broken core files of a multithreaded application on Linux. I pointed him to this LWN article (section: "write(), thread safety, and POSIX")
to emphasize that Linux has several
shortcomings in this area and told him that he might be hitting one of them.
In the following discussion (never say that Linux has a weakness), someone pointed out that writing to one and the same file from multiple threads without explicit mutual exclusion is a 'fringe case'
. Therefore, his reasoning was that, although Linux was in violation of the SUS spec, support for atomic writes wouldn't be worth the effort. I remind him that SUS requires write, not only to be thread-safe, but additionally, to be async-signal-safe, and that one cannot ensure atomicity of writes within signal handlers using mutexes. His response was that using write from a signal was bad design and therefore also need not be supported. After saying that writing log messages or transactions to a file from multiple threads are applications that really need this support, especially when file descriptors are shared among independent processes, I didn't get another answer.
Obviously one can argue about fixing this issue, because most applications can work around it. So, while I believe any violation of the SUS spec is a bug that should be fixed, there are people, especially among the Linux kernel developers, that disagree. So it really seems to be a matter of taste.
Thinking about this for some time, this situation strongly reminded me of people, who believe that their 10 Euro bathroom scales from the discounter around the corner, really has 100g precision or the one in the kitchen even 1g. Yes, they show weight in a 1g or 100g granularity, but the precision is usually (unless you spend a premium) much worse. Even, if one had a kilogram of diamonds or gold, you wouldn't be weighing them with such a scale, would you? ;-)
The same accounts for drills: you can get a percussion drill for as low as 30 Euro. The other end is possibly available from Hilti, who is able to charge two to three thousand Euro. But the professional ones are made for running continuously 10 hours a day, 365 days a year, the specified output power really goes into drilling and not into creating noise, and more.
OSs nowadays come for free, so they all seem to have the same price. But although Linux has a system call named write, it doesn't mean that it conforms to POSIX although it might even have the same API. Remember, write can be implemented differently, as weighing masses can be, too. So why not use the right tool for the problem at hand? Have you ever heard of a craftsman being fond of working with a 50 Euro drill from a discounter? Some might have tried and they stay really silent, because they found out that it is much more expensive, if you take the
shortcomings into account.
So work like a professional craftsman and employ an OS that is build for multithreaded applications, makes guarantees about write and every other systemcall, comes with detailed documentation and source code. Use Solaris.
P.S.: Click here
to take a look at the write system call of Solaris, which obviously realizes mutual exclusion for multiple threads:
P.P.S.: Concerning precision - high resolution time stamp counter are synchronized on SPARC, but not on x86.