c++ - Why is this pointer needed when calling std::call_once()? -


"C ++ concurrency in action" in book "§3.3.1, when starting a thread-safe lazy launch of a class member From std :: call_once () , it gives the following example:

  #include   

, the third parameter is the parameter passing through the function X :: open_connection () . is given to std :: call_once () when there is no input parameter for X :: open_connection () this Need Pointer?

  std :: call_once (connection_init_flag, and x :: :: open_connection, this);   

PS: Removing indicator will cause C 2064 error:

Error C2064: term Do not evaluate 0 logic in action


UPDATED: The problem is more explicitly in the book "C + + Concurrency "" When to start the same work, then std :: async :

If the first argument ( std :: call_once ) Is an indicator for a member function, the second argument ( std :: call_once to be third) provides the object on which the member functions (either directly or, an indicator, or std :: ref ) is wrapped, and the remaining argument is used for member functions . Otherwise, the second ( Std :: call_once ) and subsequent logic functions or callable object specification are passed as arguments Are the first arguments as AD.

Why is this indicator necessary when calling STD: call_once ()?

Because open_connection is a non-static data member, it has to be called on some, and such a similar example is, this (Technically, the first parameter for this in the non-static member functions is an inherent first parameter). )

This could have been applied with a different example, although in this case it would not be understandable:

  xx; Std :: call_once (connection_init_flag, & x :: open_connection, & amp; x);    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -