In book "C++ Concurrency in Action" §3.3.1, when introducing thread-safe lazy initialization of a class member using std::call_once()
, it gives the following example:
#include <mutex>
struct connection_info
{};
struct data_packet
{};
struct connection_handle
{
void send_data(data_packet const&)
{}
data_packet receive_data()
{
return data_packet();
}
};
struct remote_connection_manager
{
connection_handle open(connection_info const&)
{
return connection_handle();
}
} connection_manager;
class X
{
private:
connection_info connection_details;
connection_handle connection;
std::once_flag connection_init_flag;
void open_connection()
{
connection=connection_manager.open(connection_details);
}
public:
X(connection_info const& connection_details_):
connection_details(connection_details_)
{}
void send_data(data_packet const& data)
{
std::call_once(connection_init_flag,&X::open_connection,this);
connection.send_data(data);
}
data_packet receive_data()
{
std::call_once(connection_init_flag,&X::open_connection,this);
return connection.receive_data();
}
};
int main()
{}
From its doc, the third parameter is the parameter passing to the function X::open_connection()
. Why is this
pointer needed here when calling std::call_once()
given that X::open_connection()
has no input parameter?
std::call_once(connection_init_flag,&X::open_connection,this);
P.S.: Removing this
pointer will cause C2064 error:
error C2064: term does not evaluate to a function taking 0 arguments
Updated: This issue is further addressed clearly in §4.2.1 of book "C++ Concurrency in Action" when introducing similar functions i.e. std::async
:
If the first argument (should be the second one for
std::call_once
) is a pointer to a member function, the second argument (should be the third one forstd::call_once
) provides the object on which to apply the member function (either directly, or via a pointer, or wrapped instd::ref
), and the remaining arguments are passed as arguments to the member function. Otherwise, the second (should be the third one forstd::call_once
) and subsequent arguments are passed as arguments to the function or callable object specified as the first argument.
this->open_connection()
, right? – Uniaxial