Boost logo

Boost Users :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-12-28 09:40:10


Sebastian Redl wrote:
> What makes me believe it is that there will ever only be one writer.

Your code does not enforce this in any way.

Besides this it never is safe to access any shared data item from
multiple threads, even if it is for reading only. The only exception is
when you know for sure, that the access is atomic. The C++ standards
does not give you any guarantees on this. (But work is in progess.)

Also your code might suffer from another problem:

What does guarantee that the thread has not already ended, when you are
calling into signal_worker. i->second might point to released memory at
this time.

About volatile: while it might not hurt in your usage case, believing
volatile is enough for multiprocessor is wrong. Volatile does not give
you any guarantees on memory ordering.

To summarize:
Your suggested code might work on a specific platfform/compiler but is
nonportable, and thus dangerous. If you absolutely need it, you should
properly encapsulate it or mark it clearly as such.

Roland


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net