Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #1043
| Date | 2012-09-02 10:13 +0200 |
|---|---|
| From | Marcel Müller <news.5.maazl@spamgourmet.org> |
| Newsgroups | comp.programming.threads |
| Subject | Re: Who needs acquire semantics for read operations? |
| References | <f5a18d06-5413-4bef-93f9-96686cd08bb9@k20g2000vbk.googlegroups.com> <504244da$0$6576$9b4e6d93@newsspool3.arcor-online.net> <c03062ea-577a-4113-ad9e-043c473b4221@q20g2000vbx.googlegroups.com> |
| Message-ID | <5043152a$0$6576$9b4e6d93@newsspool3.arcor-online.net> (permalink) |
| Organization | Arcor |
On 02.09.12 09.10, Rani Sharoni wrote:
> Disabling the optimizer is sometime essential for correctness:
> LONG InterlockedAddNonNegative(__inout LONG volatile* plValue, __in
> LONG const lAdd)
> {
> LONG lValue = 0;
>
> for (;;) {
> lValue = *plValue; // volatile plValue suppress compile
> optimizations in which lValue is optimized out hence MT correctness is
> broken
> if (lValue< 0 || lValue ==
> InterlockedCompareExchange(plValue, lValue + lAdd, lValue)) break;
> }
>
> return lValue;
> }
>
> BTW - do you know about any CPU architecture might perform such
> temporary variable elimination hence break the above code?
Hmm, this is uncommon, because accessing the memory unnecessarily is no
suitable speed optimization. But the CPU caches of different cores could
be incoherent. And as long as you do not force acquire semantics on the
first read, the architecture may not beware of this. However, normally
the only impact should be another turn in the CAS loop. But you might
run into the ABA problem. In fact CAS loops are always vulnerable by the
ABA problem, but the probability increases dramatically when the cache
come into play.
> (InterlockedCompareExchange is also full membar).
I think acquire semantics is required at the first read access to be
safe, but I am not sure.
Marcel
Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar
Re: Who needs acquire semantics for read operations? Marcel Müller <news.5.maazl@spamgourmet.org> - 2012-09-01 19:24 +0200
Re: Who needs acquire semantics for read operations? Rani Sharoni <ranisharoni75@gmail.com> - 2012-09-02 00:10 -0700
Re: Who needs acquire semantics for read operations? Marcel Müller <news.5.maazl@spamgourmet.org> - 2012-09-02 10:13 +0200
Re: Who needs acquire semantics for read operations? Václav Zeman <v.haisman@sh.cvut.cz> - 2012-09-04 04:31 -0700
Re: Who needs acquire semantics for read operations? Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-09-04 15:24 +0200
Re: Who needs acquire semantics for read operations? Rani Sharoni <ranisharoni75@gmail.com> - 2012-09-04 23:41 -0700
Re: Who needs acquire semantics for read operations? Toby Douglass <a@b.com> - 2012-09-10 09:41 +0200
csiph-web