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.