Lock free list with sync_val_compare_and_swap
===

I'm trying to provide a thread safe way to update an object.  I've seen a few posts about it, but nothing that answers my question 100%.

What I want to do is maintain a pointer called 'm_First' to point at my data.  If someone calls get, I deference that pointer, returning a reference to the object it points at.

If Someone calls set, passing in a pointer to a new object, we add that pointer to our list and update m_First to point at the new object.

I store the objects in a vector, so that we can clean up memory when we are done.

Currently I have these members defined:

    std::vector<MyObject*> m_List;
    const MyObject* m_First;

First is intialized like this.
    m_First = m_List.back();

I want to allow multiple threads to read m_First, while multiple threads also write to m_First.


My class has two methods 
    MyObject& getMyObject() {return *m_First;}

    void setMyObject(MyObject* newObj) {

      m_List.push_back(newObj); // add to list so we can later cleanup mem

      do {
        Node* oldHead = m_First;
      } while (!__sync_bool_compare_and_swap(m_First, *oldHead, *newObj));
    }

What I am wondering is, returning *m_First in the get method thread safe.  i.e. Can I safely dereference this and return the reference to the object, while another thread is attempting to update m_First, or do I need to use some sort of atomic method for reading m_First.

Any advice/help would be appreciated.


  