gpt4 book ai didi

c++ - 将函数传递给新线程时编译器错误?

转载 作者:搜寻专家 更新时间:2023-10-31 00:14:59 25 4
gpt4 key购买 nike

有人可以帮我解决我在下面的 main 中创建线程时遇到的错误吗?

#include "lock_free_queue.h"
#include "Consumer.h"
#include <thread>

int main(){
lock_free_queue* my_queue = new lock_free_queue();
Consumer* c = new Consumer(my_queue);

//Error occurs here I think
std::thread t3(&Consumer::start);
...
t3.join();

Consumer.h(它都在标题中,因为最初要使用模板):

#ifndef CONSUMER_H
#define CONSUMER_H

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

class Consumer{
public:

Consumer(lock_free_queue* queue) : the_queue(queue){}

void start(){
consume = true;
while(consume){
while(the_queue->getSize()==0){}
process();
}
}

我收到这个错误(看起来很可怕):

1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152): error C2064: term does not evaluate to a function taking 0 arguments
1> class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152) : while compiling class template member function 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()(void)'
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__cdecl Consumer::* )(void),void,Consumer,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Nil,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\thr/xthread(195) : see reference to function template instantiation 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()(void)' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__cdecl Consumer::* )(void),void,Consumer,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Nil,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\thread(52) : see reference to class template instantiation 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__cdecl Consumer::* )(void),void,Consumer,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Nil,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> Main.cpp(10) : see reference to function template instantiation 'std::thread::thread<void(__cdecl Consumer::* )(void)>(_Fn)' being compiled
1> with
1> [
1> _Fn=void (__cdecl Consumer::* )(void)
1> ]

最佳答案

Consumer::start 是一个非静态成员函数。这意味着它需要一个 Consumer 实例来执行。形式上,这是通过为非静态成员函数提供 this 的隐式第一个参数来完成的。 std::thread 希望您传递一些东西,它可以用作第一个参数的参数。

因此,您需要传递一个 Consumer 实例或一个指向实例的指针,以便成员函数作用于:

std::thread t3(&Consumer::start, c);

关于c++ - 将函数传递给新线程时编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21078084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com