Help porting a super simple Processing sketch that uses a thread to ofx with ofThread

Hello everyone, Im trying to port a P5 sketch that all it does is it writes to a fifo, here’s the P5 sketch:

PVector mouse = new PVector();
 
void setup() {
  size(displayWidth/3, displayHeight/3);
  fill(0);
}
 
void draw() {
  background(255);
  mouse.set(mouseX, mouseY);
  ellipse(mouse.x, mouse.y, 30, 30);
  thread("writeToPipe");
}
 
 
void writeToPipe() {
  saveStrings("../myfifo", str(mouse.array()));
}

here’s a video of what Im trying to achieve:
https://www.youtube.com/watch?v=bB2QQrfb36s

the idea behind using a thread is for getting the program to not stall while there is nothing reading the pipe.
This is what I have in ofx:

#include "ofMain.h"
#include <iostream>

#define width 400
#define height 400

using namespace std;

class ofApp : public ofBaseApp{
    
    ofPoint p;
    ofstream myfile;

    
public:

    void setup()
    {
        ofBackground(255,255,255);
        ofSetColor(0,0,0);
    }
    

    void update()
    {
        p.set(ofGetMouseX(), ofGetMouseY());
        myfile.open ("/Users/JLafarga/Documents/of_v0.8.1_osx_release/apps/myApps/FIFO_test/bin/data/myfifo");
        myfile << ofToString(p.x) <<"\n"<<ofToString(p.y)<<"\n";
        myfile.close();
    }
    

    void draw()
    {
        ofCircle(p.x, p.y, 50);
    }
    
    
    void keyPressed(int key) {}
    void keyReleased(int key) {}
    void mouseMoved(int x, int y ) {}
    void mouseDragged(int x, int y, int button) {}
    void mousePressed(int x, int y, int button) {}
    void mouseReleased(int x, int y, int button) {}
    void windowResized(int w, int h) {}
    void gotMessage(ofMessage msg) {}
    void dragEvent(ofDragInfo dragInfo) {}
};


int main() {
	ofSetupOpenGL(width, height, OF_WINDOW);
	ofRunApp(new ofApp());
}

I have no idea where to go from there, the ofThread documentation is not all that great. Please help!

Hi @JLafarga,

First, I assume you already had a look at the documentation on ofThread.

Basically, you need to create a new class that inherits from ofThread and override the function threadedFunction().
Something like:

class MyThread : public ofThread {
    ...
    // the thread function
    void MyThread::threadedFunction() {
 
        // start
 
        while(isThreadRunning()) {
          lock();
          int bufferSize = buffer.size();
          unlock()
          if(bufferSize > 0)
          {
             writeToPipe();
          }else
         {
            sleep(period);
         }
        }
        // done
    }
void writeToPipe()
{
   lock();
   //do whatever you want, stream, etc.
   unlock();
}
public:
   void addData(ofPoint p)
   {
      lock();
      buffer.push_back(p);
      unlock();
   }
};

You can control at what rate your thread runs by calling sleep() at the end of the wile loop. Yield is similar to sleep(1), telling the thread to sleep for 1 ms. Note the use of lock() and unlock(), to prevent concurrency between the main thread and MyThread.

Then you need to instantiate your new thread in ofApp and start it. Also, push new data to the buffer.

void ofApp::setup() {
 
    // start the thread
    thread.startThread(true, false);    // blocking, non verbose
}
 
void ofApp::update() {
    p.set(ofGetMouseX(),ofGetMouseY());
    thread.addData(p);
}
 
void ofApp::exit() {
 
    // stop the thread
    thread.stopThread();
}

Hope this helps !