当从静态方法调用非静态Attach时,使用std::packaged_task对CAsyncSocket Detach套



我正在实现代码,这样我就可以在网络连接到达时接受它们,将它们从到达套接字中分离,创建一个std::packaged_task任务,在deque容器中对该任务进行排队,然后在其任务线程中执行这些任务。Bo在YouTube上的"C++线程#9:packaged_task"演讲让这一切变得容易起来,它展示了如何做到这一点。

#include "stdafx.h"
#include <afxsock.h>
#include <condition_variable>
#include <deque>
#include <future>
std::condition_variable notifyDequeNotEmptyCondVar;
std::mutex decodeMu;
class MyRxDecode : public CAsyncSocket
{
public:
static std::deque< std::packaged_task< bool() > > rxAcceptedTasks;
static bool StartDecode( SOCKET socket )
{
bool result = true;
// Attach detached socket to this socket
//result = Attach( socket ); //  error C2352: 'CAsyncSocket::Attach': illegal call of non-static member function
return result;
}
static bool DecodeTaskThread()
{
std::packaged_task< bool() > DecodingTask;
{
std::unique_lock< std::mutex > dequeLocker( decodeMu ); // makes sure all deque actions are atomic
notifyDequeNotEmptyCondVar.wait( dequeLocker, [] () { return !rxAcceptedTasks.empty(); } ); // wait until notified that deque is not empty
DecodingTask = std::move( rxAcceptedTasks.front() );
rxAcceptedTasks.pop_front();
}
DecodingTask(); // has no arg because the arg was previously bound to the functor passed in
return true;
}
};

class MyListener : CAsyncSocket
{
virtual void OnAccept( int nErrorCode ) // is called when other socket does a connect on this socket's endpoint
{
CAsyncSocket syncSocket; // msdn prescribes creating stack socket
if( Accept( syncSocket ) )
{
AsyncSelect( FD_READ | FD_CLOSE ); // msdn
SOCKET socket = syncSocket.Detach(); // msdn
// Bo Qian's lecture explains how this packaged task code works and is made thread safe.
// Create task in separate thread to process this connection and push onto deque. The main advantage of a packaged task compared to using a functor is the former links the callable object to a future, which is useful in a multi-threaded environment (Bo Qian).
std::thread decodeThread( MyRxDecode::DecodeTaskThread ); // pass-by-value ctor
std::packaged_task< bool() > rxAcceptTask( std::bind( MyRxDecode::StartDecode, socket ) ); // binds function with its param to create functor wh is passed to packaged task's ctor
std::future< bool > rxAcceptTaskFuture = rxAcceptTask.get_future();
{
std::lock_guard< std::mutex > locker( decodeMu );
MyRxDecode::rxAcceptedTasks.push_back( std::move( rxAcceptTask ) );
}
notifyDequeNotEmptyCondVar.notify_one();
bool taskResult = rxAcceptTaskFuture.get();
decodeThread.join();
CAsyncSocket::OnAccept( nErrorCode ); // msdn
}
}
};
std::deque< std::packaged_task< bool() > > MyRxDecode::rxAcceptedTasks;
int main()
{
return 0;
}

在我的情况下,代码没有编译,因为我的StartDecode是一个试图调用非静态Attach的静态方法。StartDecode是一个静态方法,因为std::bind用于将"socket"绑定到任务socket"通常会传入StartDecode方法,但为了使打包任务中的"future"正常工作,必须使用std::bind提前绑定任何传入的参数。但是,一旦StartDecode变为静态,对CAsyncSocket的Attach的非静态调用就会导致错误C2352。

如何从静态MyRxDecode::StartDecode调用非静态Attach方法?有没有一种方法可以避免在不使套接字参数变为静态的情况下将其绑定到任务?

std::bind允许您调用非静态成员函数。请参阅cppreference.com->std::bind:

Notes
… when invoking a pointer to non-static member function or pointer to  
non-static data member, the first argument has to be a reference or pointer (including, 
possibly, smart pointer such as std::shared_ptr and std::unique_ptr) to an object whose 
member will be accessed. 

用以下内容替换上面代码中rxAcceptTask的定义:

std::packaged_task< bool() > rxAcceptTask( std::bind( &MyRxDecode::StartDecode, &myRxDecode, socket ) );

允许bool StartDecode(SOCKET套接字)成员函数变为非静态。

最新更新