【发布时间】:2019-04-19 19:47:53
【问题描述】:
我正在尝试执行创建 packaged_task 队列的程序,并尝试在单独的线程中执行 packaged_tasks。我收到以下错误
错误 C2664 'std::function<_ret>::function(std::function<_ret> &&)':无法从 'std::_Binder 转换参数 1 ' 到 'std::nullptr_t'
我的代码如下所示。任何人都可以指导为什么会发生此错误吗?
/*
****************************************************************************************************
The Header File
****************************************************************************************************
*/
class CommandChannel {
private:
std::thread commChannelThread;
std::mutex commChannelmu;
std::condition_variable commChannelcv;
struct taskRequest
{};
struct taskResponse
{};
/* Creates a queue of packaged tasks that accepts one iu */
std::deque< std::packaged_task<int()>> comm_ch_task_queue;
void threadHandler(void);
protected:
int p_impl_theAdd(int a, int b);
int p_impl_theMul(int a, int b);
int p_impl_theDiv(int a, int b);
public:
/*Constructor and the Destructor*/
CommandChannel();
~CommandChannel();
/*The public functions */
int theAdd(int a, int b);
int theMul(int a, int b);
int theDiv(int a, int b);
};
/*
****************************************************************************************************
The Implementation File
****************************************************************************************************
*/
/* Implementation Functions */
int CommandChannel::p_impl_theAdd(int a, int b)
{
return a+b;
}
int CommandChannel::p_impl_theMul(int a, int b)
{
return a*b;
}
int CommandChannel::p_impl_theDiv(int a, int b)
{
return a / b;
}
/* COnstructors and Destructors */
CommandChannel::CommandChannel()
{
/*Creating a new thread that runs the threadHandler function*/
commChannelThread = std::thread(&CommandChannel::threadHandler, this);
}
CommandChannel::~CommandChannel()
{
if (commChannelThread.joinable()) {
commChannelThread.join();
std::cout << "Command Channel Thread Joined " << std::endl;
}
else
std::cout << "Problem in joining the Command Channel Thread" << std ::endl;
}
/* User Public Functions */
int CommandChannel::theAdd(int a, int b)
{
/* Creating the packaged task with the the implementation pointer */
std::packaged_task<int()> t(std::bind(&CommandChannel::p_impl_theAdd, a, b));
/* Pushing the task in the queue */
{
std::lock_guard<std::mutex> locker(commChannelmu);
comm_ch_task_queue.push_back(t);
commChannelcv.notify_one();
}
/* creating the placeholder for the return value */
std::future<int> fu = t.get_future();
/* getting the value from the future */
return fu.get();
}
int CommandChannel::theMul(int a, int b)
{
/* Create the packaged task with the pimpl */
std::packaged_task<int()> t(std::bind(&CommandChannel::p_impl_theMul, a, b));
/* Pushing the task in the queue */
{
std::lock_guard<std::mutex> locker(commChannelmu);
comm_ch_task_queue.push_back(t);
commChannelcv.notify_one();
}
/* Creating the placeholder for the return value */
std::future<int> fu = t.get_future();
/*getting the value from the future*/
return fu.get();
}
int CommandChannel::theDiv(int a, int b)
{
/* Create the packaged tasks with the pimpl */
std::packaged_task<int()> t(std::bind(&CommandChannel::p_impl_theDiv, a, b));
/*Pushing the task in the queue thorigh the mutex locks*/
{
std::lock_guard<std::mutex> locker(commChannelmu);
comm_ch_task_queue.push_back(t);
commChannelcv.notify_one();
}
/* Creating the placeholder for the return value */
std::future<int> fu = t.get_future();
/*getting the value from the future*/
return fu.get();
}
/*
Thread Handler
Pops the elemetns from the queue and then executes them
the value goes to the called function through future references
*/
void CommandChannel::threadHandler()
{
std::packaged_task<int()> t;
{
std::unique_lock<std::mutex> locker(commChannelmu);
commChannelcv.wait(locker);
t = std::move(comm_ch_task_queue.front());
comm_ch_task_queue.pop_front();
}
t();
}
/*
****************************************************************************************************
Main
****************************************************************************************************
*/
int main()
{
CommandChannel api;
api.theAdd(2, 4);
api.theDiv(6, 3);
api.theMul(5, 7);
return 0;
}
【问题讨论】:
-
不要使用绑定,而是使用 lambda。
标签: c++ multithreading api sockets c++11