Is it possible to save an ofMesh asynchronously?

The program I’m working on frequently adds polygons to a mesh and saves it as a backup. Every time it saves it, the frame rate drops for a second (the resulting .ply file will probably get up to 40 Mb).

Would it be hard to save the mesh in a different thread? Could something go wrong by doing that?

it should be fine unless you modify the mesh while saving it. if you need to modify the mesh while it’s being saved you can use ofThreadChannel to safely send the mesh to the aux thread to save it like:

//.h
class ofApp: public ofAppBase, public ofThread{
    ....
    ofThreadChannel<ofMesh> channel;
}

//.cpp
void ofApp::update(){
    //modify mesh
    channel.send(mesh);
}

void ofApp::threadedFunction(){
    ofMesh mesh;
    while(channel.receive(mesh)){
        mesh.save(file.ply);
    }
}

the channel also acts as a queue so if a save is still happening when you send a new mesh for saving it will just queue up and get saved as soon as the previous is done

Thank you! So simple! I only had to add

startThread() 

inside setup, and

channel.close();
waitForThread(true);

inside exit(). It’s happily saving 14 Mb files every second (testing) without any hicups :slight_smile:

also be sure you are compiling in release, it should be much faster then the default debug