Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;简单线程启动问题_C++_Pthreads - Fatal编程技术网

C++ C++;简单线程启动问题

C++ C++;简单线程启动问题,c++,pthreads,C++,Pthreads,我有相当简单的代码,我发现的每个示例看起来都非常相似。我确信我遗漏了一些基本的东西,所以任何帮助都将不胜感激 /*------------------------------------- State --------------------------------------*/ void Receiver::start(int num_threads = 1) { _kill_mtx.lock(); _kill_state = false; _kill_mtx.unl

我有相当简单的代码,我发现的每个示例看起来都非常相似。我确信我遗漏了一些基本的东西,所以任何帮助都将不胜感激

/*-------------------------------------
State
--------------------------------------*/
void Receiver::start(int num_threads = 1) {
    _kill_mtx.lock();
    _kill_state = false;
    _kill_mtx.unlock();
    for (int i=0; i<num_threads; ++i)
        threads.push_back(std::thread(thread_func));  // LINE 24

    std::cout << threads.size() << " now running.\n";
    std::cout << std::thread::hardware_concurrency() << " concurrent threads are supported.\n";
}

void Receiver::stop() {

}

/*-------------------------------------
Thread
--------------------------------------*/
void Receiver::thread_func() {
    while(true) {
        if (_kill_mtx.try_lock()) {
            if (_kill_state) {
                break;
            }
            _kill_mtx.unlock();
        }
        std::cout << "Still Alive!" << std::endl;
    }
}

GCC编译器是我正在使用的SDK提供的ARM编译器。到目前为止,它支持我尝试过的大多数其他C++11功能,因此我认为这不是问题所在,但我不确定。

我想,
Receiver::thread_func
是一个非静态成员函数,在这种情况下,需要传递隐式的第一个参数。假设希望线程在同一实例中运行,则需要传递
this
。否则,您需要将指针传递给另一个
接收器
实例:

threads.push_back(std::thread(&Receiver::thread_func, this));

什么是
threads
thread\u func
是一个非静态成员函数?这是有效的。我在发帖一分钟后就明白了,但我不明白为什么。现在这是有道理的。非常感谢。
threads.push_back(std::thread(&Receiver::thread_func, this));