[lkml]   [2005]   [Aug]   [26]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
    SubjectRe: [OT] volatile keyword
    On Thu, 25 Aug 2005 13:44:55 -0700 (PDT)
    Vadim Lobanov <> wrote:

    > int main (void) {
    > pthread_t other;
    > data.lock = 1;
    > data.value = 1;
    > pthread_create(&other, NULL, thread, NULL);
    > while ((volatile unsigned long)(data.lock));
    > printf("Value is %lu.\n", data.value);
    > pthread_join(other, NULL);
    > return 0;
    > }

    The "correct" way should be:

    while (*(volatile unsigned long*)(&data.lock));

    With only: "while ((volatile unsigned long)(data.lock))" GCC isn't
    forced to read to memory simply because "data.lock" isn't volatile.
    What than you do with "data.lock" value doesn't change anything. IOW
    you should get the same assembly code with and without the cast.


    "(volatile unsigned long)(data.lock)" means:
    - take the value of "data.lock" (that isn't volatile so can be
    - cast it to "volatile" (a no-op, since we already HAVE the

    "*(volatile unsigned long*)(&data.lock)":
    - take the address of "data.lock"
    - cast it to "volatile"
    - read from _memory_ the value of data.lock (through the
    volatile pointer)

    Other ways can be:
    - use read memory barrier:
    while (data.lock)
    - use everything that implies a memory barrier (eg: locking...)

    PS: everything I've said is rigorously NOT tested. :-)

    Paolo Ornati
    Linux 2.6.13-rc7 on x86_64
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to
    More majordomo info at
    Please read the FAQ at

     \ /
      Last update: 2005-08-26 09:25    [W:0.027 / U:55.244 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site