IA-32 Intel® Architecture Optimization
7-32
• Objects allocated dynamically by different threads may share cache
lines. Make sure that the variables used locally by one thread are
allocated in a manner to prevent sharing the cache line with other
threads.
Another technique to enforce alignment of synchronization variables
and to avoid a cacheline being shared is to use compiler directives when
declaring data structures.
Other techniques that prevent false-sharing include:
• Organize variables of different types in data structures (because the
layout that compilers give to data variables might be different than
their placement in the source code).
• When each thread needs to use its own copy of a set of variables,
declare the variables with:
— the directive threadprivate, when using OpenMP
— the modifier __declspec (thread), when using Microsoft
compiler
Example 7-6 Placement of Synchronization and Regular Variables
int regVar;
int padding[32];
int SynVar[32*NUM_SYNC_VARS];
int AnotherVar;
Example 7-7 Declaring Synchronization Variables without Sharing a Cache
Line
__declspec(align(64)) unsigned __int64 sum;
struct sync_struct {…};
__declspec(align(64)) struct sync_struct sync_var;