
"Chris M. Thomasson" <cristom@charter.net> wrote in message news:hgoao1$klu$1@ger.gmane.org... [...]
which provides a nice, contention-free (no stores by readers!) mechanism to share read-mostly state.
Interesting... Humm, that kind of reminds me of a sequence lock:
<membars aside for a moment> _______________________________________________________________ template<typename T> struct sequence_lock { T m_object; // = T() uint32_t m_version; // = 0 mutex m_mutex;
T read() { for (;;) { if (LOAD(&m_version) % 2) yield(), continue;
T object = m_object;
if (LOAD(&m_version) % 2) yield(), continue;
I forgot to load and compare the version! let me fix that: ___________________________________________________________ T read() { for (;;) { uint32_t version1 = LOAD(&m_version); if (! (version1 % 2)) { T object = m_object; uint32_t version2 = LOAD(&m_version); if (version1 == version2) { return object; } } backoff(); } } ___________________________________________________________ Sorry about that! ;^o
void write(T const& object) { mutex::scoped_lock lock(m_mutex);
STORE(&m_version, LOAD(&m_version) + 1);
m_object = object;
STORE(&m_version, LOAD(&m_version) + 1); } }; _______________________________________________________________