Search code examples
c++boost-thread

boost threads - is scoped_ptr::reset() an atomic operation?


This is the first time I'm using threads, and I'm doing it with boost. The situation is that I have a big array of 3d polygon meshes for which I build an octree (one tree per mesh). I'd like to execute the build process in parallel, and immediately go to openGL display loop - without waiting for the builds to complete. (The object simply won't display anything until its octre is complete).

I use boost::scoped_ptr::reset() to attach octree data to my objects. Can I consider reset() method to be atomic operation in threaded execution? If not what should I look after ?

Below is the pseudo code depicting what I'm doing. The actual code works as expected, but I experience occasional crashes (may be related to smth else.)

class BigData
{
private:
    boost::scoped_ptr<float*> p_data_;

public:
    void Compute() // this will run in threads
    {
        float* p_temp = new float [1000];
        DoComputation(p_temp);
        p_data_.reset(p_temp); // atomic ?
    }
    void operator()() {Compute();}

    void Display() // do nothing if p_data_ is not ready
    {
        if(p_data_)
            DoDisplay();
    }
}


int main()
{
    std::vector<BigData>    objects_arr(1000);

    // run Compute() in threads
    for(int i=0; i<objects_arr.size(); ++i)
        boost::thread comp_thread( objects_arr[i] );

    // immediately go to display
    while(true)
    {
        for(int i=0; i<objects_arr.size(); ++i)
            objects_arr[i].Display();
    }

}

Solution

  • Nope. This isn't really an issue with boost::scoped_ptr so much as a problem with pointer assignment in general.

    Is pointer assignment atomic in C++?