在 运行 时将参数传递给 std::bind 到调度队列
Pass argument to std::bind at run time to a dispatch Queue
我无法将参数传递到我的调度队列,该队列采用函数指针作为参数。我已经实现了一个像 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()));
调度函数定义为:
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 (AudioRecordEngine::*)(const std::__ndk1::array &), std::__ndk1::array > >' to 'fp_t'(又名 'function<basic_string<char, char_traits, allocator > (const array<float, kMaxSamples> &)>')
看来我的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;
}
在DispatchQueue
的实现中,fp_t
类型的函数对象被调用为fn()
,所以fp_t
应该是std::function<std::string()>
或 std::function<void()>
。应该没有 const std::array&
参数。
std::bind
应该给你一些不带参数的东西。您的 std::bind
几乎是正确的。当你想调用一个 non-static 成员函数时,你需要一个对象。该对象,以指针或引用的形式,应该是 std::bind
:
的第二个参数
AudioRecordEngine engine;
queue->dispatch(std::bind(
&AudioRecordEngine::run,
std::ref(engine),
mRecordingCallbackImp.getAudioData()
));
注意engine
生命周期。
或者,您可以使用 lambda 函数代替 std::bind
:
AudioRecordEngine engine;
queue->dispatch([&] {
engine.run(mRecordingCallbackImp.getAudioData());
});
这种方法和之前的方法在 getAudioData()
被调用的那一刻有所不同:在 2 中,getAudioData()
执行的结果存储在 std::bind
返回的函数对象中,在3, getAudioData()
在 run
被调用之前被调用。
我无法将参数传递到我的调度队列,该队列采用函数指针作为参数。我已经实现了一个像 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()));
调度函数定义为:
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
看来我的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;
}
在
DispatchQueue
的实现中,fp_t
类型的函数对象被调用为fn()
,所以fp_t
应该是std::function<std::string()>
或std::function<void()>
。应该没有const std::array&
参数。
的第二个参数std::bind
应该给你一些不带参数的东西。您的std::bind
几乎是正确的。当你想调用一个 non-static 成员函数时,你需要一个对象。该对象,以指针或引用的形式,应该是std::bind
:AudioRecordEngine engine; queue->dispatch(std::bind( &AudioRecordEngine::run, std::ref(engine), mRecordingCallbackImp.getAudioData() ));
注意
engine
生命周期。或者,您可以使用 lambda 函数代替
std::bind
:AudioRecordEngine engine; queue->dispatch([&] { engine.run(mRecordingCallbackImp.getAudioData()); });
这种方法和之前的方法在
getAudioData()
被调用的那一刻有所不同:在 2 中,getAudioData()
执行的结果存储在std::bind
返回的函数对象中,在3,getAudioData()
在run
被调用之前被调用。