Search code examples
c++multithreadingvisual-studio-2015createthread

Multithreading non-static in class C++


I am developing an application for Windows in which required run three processes:_thread_EEG (acquisition), _thread_MachineLearning (processing), _thread_Interface (interface). The second process uses data by the first process, and third process requires the result of the second process.

class uMotor{
private:
    long _endTime;

    bool _busyBuffer;
    bool _busyLabel;

    Raw  _Shared_buffer;
    char _Shared_label ;

    uEEG _gtec;
    Interface _screen;

    void _EEG            (long endTime);
    void _MachineLearning(long endTime);
    void _Interface      (long endTime);

    DWORD __stdcall _Thread_EEG(LPVOID arg){
        uMotor *yc_ptr = (uMotor*)arg;
        yc_ptr->_EEG(_endTime);
        return 1;
    }

    DWORD __stdcall _Thread_MachineLearning(LPVOID arg){
        uMotor *yc_ptr = (uMotor*)arg;
        yc_ptr->_MachineLearning(_endTime);
        return 1;
    }

    DWORD __stdcall _Thread_Interface(LPVOID arg){
        uMotor *yc_ptr = (uMotor*)arg;
        yc_ptr->_Interface(_endTime);
        return 1;
    }

public:
    uMotor();
    void BCI();
    ~uMotor();
};

The threads are called in function uMotor::BCI():

void uMotor::BCI(){
    const long NUM_SECONDS_RUNNING = 9;

    long startTime = clock();
    long endTime = startTime + NUM_SECONDS_RUNNING * CLOCKS_PER_SEC;

    HANDLE Handle_Thread_EEG             = 0;
    HANDLE Handle_Thread_MachineLearning = 0;
    HANDLE Handle_Thread_Interface       = 0;

    Handle_Thread_EEG = CreateThread(NULL, 0, _Thread_EEG, &endTime, 0, NULL);
    Handle_Thread_EEG = CreateThread(NULL, 0, _Thread_MachineLearning, &endTime, 0, NULL);
    Handle_Thread_EEG = CreateThread(NULL, 0, _Thread_Interface, &endTime, 0, NULL);
}

In the function CreateThread, Visual Studio 2015 shows an error argument of type "DWORD(_stdcall uMotor::*)(LPVOID arg)" is incompatible with parameter of type "LPTHREAD_START_ROUTINE"

What am I doing wrong?


Solution

  • The thread function must be static, so add static before DWORD __stdcall...

    Also, fourth parameter to CreateThread is the routine parameter. You are expecting pointer to uMotor, but passing &endTime instead. Replace &endTime with this.