Thread: Casting away volatile

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    83

    Casting away volatile

    Hi.

    I have a lock-free singly linked list.

    Where it uses atomic instructions to update its pointers, all the pointers are volatile.

    Volatile prevents compiler optimization.

    There are times when I would like to use the list with only one thread at a time; I enter this state and exit this state, performing the necessary operations such that it is safe to begin single threaded operations and then to resume multi-threaded operations.

    What I want to do when in single threaded mode is cast away the volatile type qualifier so that the compiler can optimize.

    I am however not at all sure if this is permitted.

    To provide a concrete example, let's say I have a list state, and it contains a pointer to a list element. That pointer is volatile, the struct it points to is not volatile, i.e.

    struct list_element * volatile head_pointer;

    I wish to access this as if it were not volatile, as I'm in single threaded mode.

    Is it safe to do this? I am concerned I may be engaging in undefined behaviour.

    struct list_element *temp;

    temp = (struct list_element *) head_pointer;

    Where head_pointer is defined as above, where the pointer itself is volatile, but the struct is not.
    Last edited by Toby Douglass; 12-30-2016 at 11:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about volatile
    By homer_3 in forum C++ Programming
    Replies: 26
    Last Post: 12-20-2014, 09:06 AM
  2. To Volatile, or not to Volatile?
    By EVOEx in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2012, 02:07 PM
  3. synchronization and volatile
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-04-2008, 08:31 AM
  4. Volatile Keyword!!
    By maven in forum C Programming
    Replies: 8
    Last Post: 12-06-2005, 12:56 PM
  5. volatile??
    By jacktibet in forum C Programming
    Replies: 2
    Last Post: 05-29-2003, 03:46 PM

Tags for this Thread