【发布时间】:2020-08-23 07:04:38
【问题描述】:
我无法将参数传递给以函数指针作为参数的调度队列。我已经实现了一个调度队列,比如this tutorial
typedef std::function<std::string( const std::array<float, kMaxSamples> &)> fp_t;
class DispatchQueue {
public:
DispatchQueue(std::string name, size_t thread_cnt = 1);
~DispatchQueue();
//move
void dispatch(fp_t && item); //Take the typedef defined above
private:
std::string _name;
std::queue<fp_t> _q;
std::vector<std::thread> _threads;
void dispatch_thread_handler(void);
std::mutex _lock;
std::condition_variable _cv;
bool _quit;
};
我的 std::function 将 std::array 作为参数。
然后,稍后在我的代码中,我将这个特定作业添加到队列中以处理此参数。
queue->dispatch(std::bind(&AudioRecordEngine::run, mRecordingCallbackImp.getAudioData()));
dispatch函数定义为:
void DispatchQueue::dispatch(fp_t &&item)
{
std::unique_lock<std::mutex> lock(_lock);
_q.push(item);
// Manual unlocking is done before notifying, to avoid waking up
// the waiting thread only to block again (see notify_one for details)
lock.unlock();
_cv.notify_one();
}`
也许这个用例太复杂了,我可能不知道如何做得更好。
非常感谢您的建议和帮助。我被困了很长一段时间。
非常感谢
编辑: 我面临的问题是在编译时:
从 '__bind<:__ndk1::__bind std::__ndk1::char_traits std::__ndk1::allocator> (AudioRecordEngine::*) 没有可行的转换(const std::__ndk1::array
似乎我的 std::function 不支持我传递的参数。这个问题看起来我没有正确使用 std::bind。
基本上我想将带有给定参数的函数指针传递给我的调度函数。
编辑 2:
AudioRecordEngine::run 定义为:
std::string AudioRecordEngine::run(const std::array<float, __NUM_SAMPLES__> & audioData) {
std::thread::id this_id = std::this_thread::get_id();
LOGD("In the thread ID %zu \n", this_id);
//double freq = FFTNativeWrapper::fftEntryPoint(audioData);
//LOGD("In the Thread, FFT analysis == %zu \n", freq);
return "from thread";
}
std::array<float, kMaxSamples> RecordingCallbackImp::getAudioData() {
return mData;
}
【问题讨论】:
-
我编辑了我的帖子,并在编译时给出了错误。如果还不清楚,请告诉我。谢谢你的时间。我不确定参数是否会正确传递给调度函数。这对我来说是一个相当复杂的问题。
-
如果您包含
AudioRecordEngine::run和getAudioData()的声明也会有所帮助。 -
std::function需要一个参数。您的std::bind不包含占位符,因此它为您提供了一个没有参数的函数(而是一个函数对象)。此外,要调用成员函数 (run),您必须提供AudioRecordEngine类型的对象作为第一个参数。 -
你如何调用来自
_q的功能对象? -
getAudioData或getRecordData?
标签: c++ parameter-passing std-function