|
Boost : |
Subject: Re: [boost] [lockfree::fifo] Review
From: Chris M. Thomasson (cristom_at_[hidden])
Date: 2010-01-04 02:12:21
"Chris M. Thomasson" <cristom_at_[hidden]> wrote in message
news:hgoao1$klu$1_at_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);
> }
> };
> _______________________________________________________________
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk