Threading help (thread stopes working)

Hiya all

please help, i have got a problem, and it is this…
I am using a threaded function to get data from an ardunio, and i works well but the thread stops working after a couple of hours. it does not cash the program.

This is the first time i have used thread so i may be doing some thing very silly.

i think i might not have the right way of getting the variables from the thread to the testApp,

this is the code from the threaded function that i think might be wrong

int getmph(){
    if( lock() ){
        unlock();
        return mph;  
    }
      }

and then i calling it from the tesApp with

mph = threadSerial.getmph();

if this is the right way of getting the variables from the thread , please let me known and i can look else where for the problem.

here is all of the code that i am using

any help would be amazing!

Best
Ben

threadSerialEvent.h

#ifndef _THREADED_OBJECT
#define _THREADED_OBJECT

#include "ofMain.h"




class threadSerialEvent : public ofThread{
    
public:
    
    
    int count;  // threaded fucntions that share data need to use lock (mutex)

    
    
    ofSerial serial;
    
    bool		bSendSerialMessage;			// a flag for sending serial
    char		bytesRead[3];				// data from serial, we will be trying to read 3
    char		bytesReadString[4];			// a string needs a null terminator, so we need 3 + 1 bytes
    int			nBytesRead;					// how much did we read?
    int			nTimesRead;					// how many times did we read?
    float		readTime;
    
    string fullMessage;
    int interMessage;
    string Sel;
    int Sensor;      // HOLDS PULSE SENSOR DATA FROM ARDUINO
    int IBI;         // HOLDS TIME BETWEN HEARTBEATS FROM ARDUINO
    float mph;         // HOLDS HEART RATE VALUE FROM ARDUINO
    bool heart;
    bool onOff;
    
    
    
    threadSerialEvent(){
        count = 0;
    }
    
    void open(int baud){
        serial.listDevices();
        vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
       // int baud = 115200;
        serial.setup(0, baud);
        interMessage = 0;
        startThread(true, false);   // blocking, verbose
    }
    
    
    void start(){
        startThread(true, false);   // blocking, verbose
    }
    
    
    void stop(){
        stopThread();
    }
    
    //--------------------------
    void threadedFunction(){
        
        while( isThreadRunning() != 0 ){
            if( lock() ){
                unsigned char inChar;
                string inString;
                string tempString;

                if (serial.available() > 0) {

                    
                    inChar = serial.readByte();
                    inString += inChar;

                    string findBreak = ofToString(inChar);
                    tempString += ofToString(inChar);
                    
                }
                
                if(inString != ""){
                    
                    vector<string> dataFromArdunio = ofSplitString(inString,",");
                    
                    for(int a = 0;a<dataFromArdunio.size();a++){
                        
                        if(dataFromArdunio[a] == "\n"){
             
                            if(Sel == "O"){
                                cout << " threaded button main " << endl;
                                onOff = true;
                                cout << " hi " << endl;
                            }
                            
                
                            
                            if(Sel == "S"){
                                
                                Sensor = ofToInt(fullMessage);
                            //    cout << " Sensor" <<  " " << Sensor  << endl;
                                //   cout << " ssssss " << " " << Sensor << " " << fullMessage << endl;
                            }
                            
                            if(Sel == "M"){
                                mph = ofToInt(fullMessage);
                          //      cout << " BPM " <<  " " << BPM  << endl;
                                heart = true;
                                //cout << Sel << " j finnish " <<  endl;
                                
                                
                            }
                            if(Sel == "Q"){
                                IBI = ofToInt(fullMessage);
                              //  cout << " IBI " <<  " " << IBI  << endl;
                            }
                            //    cout << fullMessage  << endl;
                            
                            fullMessage = "";
                            interMessage = 0;
                            Sel = "";
                            serial.drain();
                        }
                        else{
                            interMessage ++;
                            if(interMessage == 1){
                                Sel = dataFromArdunio[a];
                            }else{
                                fullMessage += dataFromArdunio[a];
                            }
                            if(interMessage > 100){
                                interMessage = 0;
                            }
                        }
                        
                        
                    }
                    
                    
                    
                    
                }
                
                unlock();

           }
        }
    }
       //--------------------------
    bool getButton(){
        if( lock() ){
            unlock();
            return onOff;
        }
        
        
        
    }
    
      //--------------------------
    int getmph(){
        if( lock() ){
            unlock();
            return mph;
            
        }
        
          }
        
    
    //--------------------------
    void draw(){
        ofSetColor(255);

        
        if( lock() ){

            unlock();
        }else{

        }

    }
    

     

    
};

#endif

and then the testApp.cpp

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

    threadSerial.open(9600);
}

//--------------------------------------------------------------
void testApp::update(){
 mph = threadSerial.getmph();


    if(threadSerial.getButton()){
        cout << " button main thread" << endl;


    }
}

//--------------------------------------------------------------
void testApp::draw(){

}

and the .h

#pragma once

#include "ofMain.h"
#include "threadSerialEvent.h"
class testApp : public ofBaseApp{
	public:
		void setup();
		void update();
		void draw();

		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 dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);


    threadSerialEvent threadSerial;

    float mph;
};

actually you don’t need to lock to return something like a float or an int, and the way you are doing it it might actually change while before you return it. doing it without locks at all should be ok but if you want to be sure that it doesn’t change while you are using it you can lock the thread from outside the class like:

threadSerial.lock();
float mph = threadSerial.getmph();
// do something with mph
threadSerial.unlock();

thanks for the info, i will give it ago,
do you think that might be the reason why the thread stopes after some time?

Best
Ben

i guess you might be having a deadlock:

it might also be that the something inside the threaded function is throwing a exception but i don’t think that’s the reason since nothing in OF usually would throw exceptions