在C ++类中编译线程时出错(错误xthread)

时间:2016-06-02 16:25:19

标签: c++ multithreading visual-studio-2015

我正在Visual Studio 2015中开发Windows应用程序。 该应用程序运行三个并行进程:_thread_EEG(获取),_ thread_MachineLearning(处理),_ thread_Interface(接口)。 我正在使用这些库:<thread> <mutex>

class uMotor{
private:
    // Shared memory status
    std::mutex _Mutex_Buffer;
    std::mutex _Mutex_Label ;

    // Shared memory
    Raw  _Shared_buffer;
    char _Shared_label ;

    long _endTime;


    void _EEG            ();
    void _ML             ();
    void _Interface      ();

    static void _Thread_EEG(void *args){
        uMotor *prunnable = static_cast<uMotor*>(args);
        prunnable->_EEG();
    }

    static void _Thread_ML(void *args){
        uMotor *prunnable = static_cast<uMotor*>(args);
        prunnable->_ML();
    }

    static void _Thread_Interface(void *args){
        uMotor *prunnable = static_cast<uMotor*>(args);
        prunnable->_Interface();
    }
   /*
                      ...
    */
}

在函数uMotor::BCI()中调用线程:

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

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

    // Create threads
    std::thread std_Thread_EEG      (_Thread_EEG      );
    std::thread std_Thread_Interface(_Thread_Interface);
    std::thread std_Thread_ML       (_Thread_ML       );

    std_Thread_EEG      .join();
    std_Thread_Interface.join();
    std_Thread_ML       .join();
}

此代码在IDE(Visual Studio 2015)中看起来很好但是当我尝试编译它时,我收到以下错误:

Error C2672 'std::invoke': no matching overloaded function found uMotor c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread 240

Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' uMotor c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread 240

我做错了什么?

2 个答案:

答案 0 :(得分:1)

线程支持已添加到C ++中,原因是为了减轻开发人员使用不安全的强制转换,将值打包到.view-animation { -webkit-transition: 1s; } 结构中的需要等等。void*您的代码应该跟随(更改一个)功能,你可以做其余的事情):

<thread>

答案 1 :(得分:0)

您没有将传递给函数的参数传递给std :: thread构造函数,只传递函数本身。这导致试图在没有参数的情况下调用_Thread_EEG(),但_Thread_EEG需要一个(我使用EEG作为示例,当然也适用于其他两个)。

你真正需要的是

uMotor instance;
std::thread eeg(&_Thread_EEG, &instance); // or use this, if BCI is not static

std :: thread的构造函数是一个模板,因此&amp; instance作为uMotor *传递给_Thread_EEG,它允许你将其声明为

static void _Thread_EEG(uMotor* instance);

你不再需要施法。不过我更喜欢参考:

static void _Thread_EEG(uMotor& instance);

然后,您需要为线程模板显式引用:

std::thread eeg(&_Thread_EEG, std::ref(instance)); // or std::ref(*this), if not static

顺便说一下:看看here关于以下划线('_')开头的标识符......