Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

False sharing in C++

I have several classes that suffer from cache contention and are allocated with the "new" operator. Can I somehow make sure that "new" returns an address aligned to a cache line?

I am using GCC (if it's not portably possible).

like image 645
jk4736 Avatar asked Feb 19 '12 19:02

jk4736


1 Answers

You can use memalign or _aligned_alloc for glibc or windows CRT systems respectively. you can even use a custom allocator like nedmalloc and have it align the blocks, which could also give you some other added bonuses.

you should also mark them with __attribute__((aligned(64))), just in case they get statically allocated.

like image 83
Necrolis Avatar answered Oct 13 '22 15:10

Necrolis