Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"07-14-11 - compare_exchange_strong vs compare_exchange_weak"

5 Comments -

1 – 5 of 5
Blogger ryg said...

I'd rather see things in the standard like "if LL-SC exist on this architecture, then they can be invoked via __ll() and __sc()"
The problem with LL/SC is that they often come with limitations that are near-impossible to express in C/C++; e.g. on some implementations, you're not allowed to have any other memory access between a LL/SC pair (doing so will cause you to lose the reservation). In ASM that's no problem, just don't do loads/stores, but how do you express it in C/C++ where there's no binding way to force a value into a register, and even seemingly innocent things like adding a literal constant might require an extra memory access to load the constant from memory?

The truth is that on the micro level, most compilers aren't aware of such limitations, even if the underlying architecture or implementation has them. You just shove it all into intrinsics that are expanded very late into the compilation process, after any optimizations that might screw them up.

July 14, 2011 at 2:07 PM

Blogger cbloom said...

All of the platforms with LL/SC provide intrinsics for LL/SC , so you have that problem anyway. It's just at the moment I have to do rigamorale like

#if PPC
__lwarx()
#elif ARMV6
__ldrex()
#else
...

granted, you're right that it is ugly in C and can produce subtly broken code, but we have that problem anyway.

This is the classic cop-out of C standardization if it's messy or varies by platform they leave it undefined. But that doesn't make the problem go away. It just makes it *worse* because now it's left up to each compiler implementation on each platform, so now you have not only a messy problem, but one that's different everywhere and badly documented, and etc..

July 14, 2011 at 5:28 PM

Blogger cbloom said...

IMO there could easily be more things in the C standards that are like :

if the platform has a native vector type then the syntax for using it is "vector[4] int x;"

and then provide a mechanism like

#if platform supports(vector[4] int)
.. simd code ..
#else
.. non-simd code ..
#endif

I dunno, this is an unrelated rant.

Like I feel like C has two parts (actually make 3 or 4 parts), like there's the core bit of basic C stuff that every platform has to support. Then there's stuff that platforms may or may not have, for example I should be able to ask things like (#if platform unaligned-load-fast) - rather than just leaving that a big undefined mess.

Anyhoo...

July 14, 2011 at 5:31 PM

Blogger Anthony Williams said...

The non-member compare_exchange_weak and compare_exchange_strong functions take their arguments by pointer, for C compatibility. It is the member functions which take references. e.g.

std::atomic<int> ai;

int old=0;

if(ai.compare_exchange_strong(old,1))
do_something();

or alternatively

if(compare_exchange_strong(&ai,&old,1))
do_something();

July 26, 2011 at 1:50 PM

Blogger cbloom said...

Ah, thanks for the clarification.

I think non-const references are pretty much always horrible.

I literally thought there was a bug in my code the first time I ported some Win32 code from InterlockedCompareExchnage to C++0x compare_exchange because it kept changing my value of "old".

In fact back when I got to use C++ and pick my own coding style, I very much liked the convention that all const objects are passed by ref and all non-const are passed by pointer; this makes it super clear at the call site which args can be mutated and which can't. I miss that code base a lot. Oh well...

July 26, 2011 at 2:06 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.