Dynamic reads without locking.

Pawel Jakub Dawidek nick at garage.freebsd.pl
Wed Oct 8 03:09:55 PDT 2003


On Wed, Oct 08, 2003 at 11:51:06AM +0200, Harti Brandt wrote:
+> You need to lock when reading if you insist on consistent data. Even a
+> simple read may be non-atomic (this should be the case for 64bit
+> operations on all our platforms). So you need to do
+> 
+> mtx_lock(&foo_mtx);
+> bar = foo;
+> mtx_unlock(&foo_mtx);
+> 
+> if foo is a datatype that is not guaranteed to be red atomically. For
+> 8-bit data you should be safe without the lock on any architecture. I'm
+> not sure for 16 and 32 bit, but for 64-bit you need the look for all
+> our architectures, I think.

But I'm not talking about non-atomic reads. What I'm want to show is that
even atomic read (without lock) is dangerous in some cases.

+> If you don't care about occasionally reading false data (for statistics or
+> such stuff) you can go without the lock.

I'm afraid that many developers thinks that atomic reads are always safe
without locks (there are many such reads in sources). I hope I'm wrong.

-- 
Pawel Jakub Dawidek                       pawel at dawidek.net
UNIX Systems Programmer/Administrator     http://garage.freebsd.pl
Am I Evil? Yes, I Am!                     http://cerber.sourceforge.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 305 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20031008/05360f25/attachment.bin


More information about the freebsd-hackers mailing list