Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #1810
| From | Andy Venikov <swojchelowek@gmail.com> |
|---|---|
| Newsgroups | comp.programming.threads |
| Subject | Re: forcing the compiler to reload from memory with c++0x |
| Date | 2011-02-03 17:43 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <iifb30$9d3$1@news.eternal-september.org> (permalink) |
| References | (8 earlier) <ihv8t6$bmr$1@news.eternal-september.org> <87oc6yxdcw.fsf@justsoftwaresolutions.co.uk> <ii745u$5q3$1@news.eternal-september.org> <87sjw8w4b5.fsf@justsoftwaresolutions.co.uk> <iietha$f3a$1@news.eternal-september.org> |
<snip>
> Correct me if I'm wrong, but what you're saying is that even in C++0x to
> write correct lock-free code we would have to use volatile?
> But as I understand, C++0x doesn't make any new additions to the meaning
> of volatile. It's still technically an UB to use volatile for threading
> and volatile really has no semantics for threads?
> I had my fair share of arguments about volatile (Joshua Maurice may
> attest to that) and why they currently (C++03) may be the only viable
> solution to write lock-free code, even though we all know that
> technically we are in the UB land. I was hoping that c++0x would
> alleviate that and not require us to use volatile.
It would also mean that the Peterson's lock implementation (Dmitri's
version) as described in your blog would be broken.
http://www.justsoftwaresolutions.co.uk/threading/petersons_lock_with_C++0x_atomics.html
Let me briefly show the code here, with some minor modification to
remove repetition:
std::atomic<int> flags[2] = {0, 0};
std::atomic<int> turn(0);
void lock(unsigned index)
{
unsigned int me = index;
unsigned int other = 1 - me;
flag[me].store(1, std::memory_order_relaxed); //1
turn.exchange(other, std::memory_order_acq_rel); //2
while (flag[other].load(std::memory_order_acquire) //3
&& other == turn.load(std::memory_order_relaxed)) //4
std::this_thread::yield();
return;
}
Since turn is not declared volatile, the implementation would be allowed
to assume that after we stored "other" in it in line 2, it won't change.
So, the turn.load on line 4 could be optimized away (or assumed to be
always true). That will create a dead-lock if both threads are entering
the critical section simultaneously. They both set their flags to 1,
they both can observe that the other flags is set, but they can't get
out of the loop because they both see turn as "other".
Off-topic question about this implementation: why do we need an exchange
in (2)? Wouldn't store suffice? Also, it looks like we don't really need
memory_order_acquire in line 3, relaxed should do.
But we still need to declare turn as volatile.
Thanks,
Andy.
>
> Thanks,
> Andy.
>
Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: forcing the compiler to reload from memory with c++0x Andy Venikov <swojchelowek@gmail.com> - 2011-01-31 14:56 -0500
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-07 22:13 +0000
Re: forcing the compiler to reload from memory with c++0x Andy Venikov <swojchelowek@gmail.com> - 2011-02-07 17:54 -0500
Re: forcing the compiler to reload from memory with c++0x Alexander Terekhov <terekhov@web.de> - 2011-02-04 16:39 +0100
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-04 17:52 +0000
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-04 08:56 +0000
Re: forcing the compiler to reload from memory with c++0x Andy Venikov <swojchelowek@gmail.com> - 2011-02-06 23:23 -0500
Re: forcing the compiler to reload from memory with c++0x Andy Venikov <swojchelowek@gmail.com> - 2011-02-03 17:43 -0500
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-04 09:04 +0000
Re: forcing the compiler to reload from memory with c++0x Joshua Maurice <joshuamaurice@gmail.com> - 2011-02-03 15:54 -0800
Re: forcing the compiler to reload from memory with c++0x Andy Venikov <swojchelowek@gmail.com> - 2011-02-03 17:58 -0500
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-01 08:24 +0000
Re: forcing the compiler to reload from memory with c++0x Anthony Williams <anthony.ajw@gmail.com> - 2011-02-04 08:47 +0000
Re: forcing the compiler to reload from memory with c++0x Andy Venikov <swojchelowek@gmail.com> - 2011-02-03 13:52 -0500
csiph-web