CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    445

    InterLocked and align

    Hi all,

    Many InteLocked...... functions like InterLockedIncremet
    http://msdn.microsoft.com/en-us/libr...14(VS.85).aspx
    have the following quote in MSDN:

    "The variable pointed to by the Addend parameter must be aligned on a 32-bit boundary; otherwise, this function will behave unpredictably on multiprocessor x86 systems and any non-x86 systems. See _aligned_malloc."

    Do I need to use _alloc_maloc for evry InterLocked...... function I use??

    for example I would like to implement a spin lock using InterlockedExchange
    http://msdn.microsoft.com/en-us/libr...90(VS.85).aspx
    Do I need to align it ???

    Can someone please explain when align is required.

    Many thanks !!!
    Last edited by Salvadoravi; October 11th, 2009 at 08:48 AM.

  2. #2
    Join Date
    May 2008
    Posts
    300

    Re: InterLocked and align

    no. it's just a way to make your data aligned, and not the only way, and not everytime you call interlocked intrinsics.

    What it tells you is that 1. you should make data aligned, 2. to make your data aligned, you might need _aligned_malloc.

    But anyway, malloc always give you data aligned to 8 byte boundary, I think... and there is no 128 bit interlocked intrinsic, I think, again...
    Nope

  3. #3
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: InterLocked and align

    The function will not perform any automatic alignment for non-aligned data for you.

    Non aligned 'int':
    Code:
    #pragma pack(1) // 1 byte alignment
    struct Test
    {
       char a;
       int b;
    };
    Here the variable 'b' at (base+1) offset to the Test struct. If you do not specify #pragma pack directive, compiler will pack the data as per project settings (either 4-byte, or 8-byte is standard). In that case 'b' would be aligned in 4-byte boundary.
    Now assume you have a function, that takes int* or int& as an argument:
    Code:
    void f(int*);
    ...
    Test t;
    t.b=100;
    f( &t.b );
    In function 'f' you might access/modify the passed variable. The variable (pointer/reference) is NOT properly-aligned and the x86 CPU will align it for you in 4-byte boundary. 64-bit processor will NOT align!

    If you pass 't.b' variable to InterlockedXX function, the passed variable (pointer) is NOT aligned properly, and thus the unpredictable behaviour!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured