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:

class ofApp: public ofAppBase, public ofThread{
    ofThreadChannel<ofMesh> channel;

void ofApp::update(){
    //modify mesh

void ofApp::threadedFunction(){
    ofMesh mesh;

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


inside setup, and


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