General Category > General Discussion about just::thread

unaligned atomic variables

<< < (2/2)

TA:
Another thing that can be done is putting atomic classes between

--- Code: ---#pragma pack(push)
#pragma pack(4)
// class atomic ...
#pragma pack(pop)

--- End code ---

Anthony Williams:

--- Quote from: TA on February 17, 2011, 02:59:07 PM ---hmmm..
#pragma pack affects default alignment, but it shouldn't affect explicit alignment.
This code outputs 8 for the size and true for is_aligned call on my machine inspite of #pragma pack(1).

--- Code: ---#include <iostream>

#pragma pack(1)

struct X
{
char a;
__declspec (align(4)) long b;
};

bool is_aligned(void *ptr, int boundary)
{
return ((uintptr_t)ptr & (boundary - 1)) == 0;
}


int main()
{
X x;
std::cout << sizeof(X) << std::endl;
std::cout << is_aligned(&x.b, 4) << std::endl;
}

--- End code ---
I'm not sure about gcc.
Anyway, if this is not portable enough then assert is good solution too.

--- End quote ---

MSVC is inconsistent: with #pragma pack(1) it obeys the alignment requirements sometimes, but not others.

gcc always ignores alignment requirements with #pragma pack(1).

I'll put asserts in the atomic ops.

Anthony Williams:

--- Quote from: TA on February 17, 2011, 03:12:46 PM ---Another thing that can be done is putting atomic classes between

--- Code: ---#pragma pack(push)
#pragma pack(4)
// class atomic ...
#pragma pack(pop)

--- End code ---


--- End quote ---

That wouldn't help: it just affects the layout of the atomic objects, not the alignment of the whole object.

TA:

--- Quote from: Anthony Williams on February 17, 2011, 03:16:06 PM ---MSVC is inconsistent: with #pragma pack(1) it obeys the alignment requirements sometimes, but not others.

gcc always ignores alignment requirements with #pragma pack(1).

I'll put asserts in the atomic ops.

--- End quote ---
Thanks.

TA:

--- Quote from: Anthony Williams on February 17, 2011, 03:18:23 PM ---
--- Quote from: TA on February 17, 2011, 03:12:46 PM ---Another thing that can be done is putting atomic classes between

--- Code: ---#pragma pack(push)
#pragma pack(4)
// class atomic ...
#pragma pack(pop)

--- End code ---


--- End quote ---

That wouldn't help: it just affects the layout of the atomic objects, not the alignment of the whole object.

--- End quote ---
Yes, that's right. Didn't think about that. Thank you.

Navigation

[0] Message Index

[*] Previous page

Go to full version