经常忘记,留个代码给自己快速理解回忆
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <boost/asio.hpp>
struct times_t // for test copy-construct and move-construct
{
times_t()
: m_count(0)
{
}
times_t(const times_t & other)
: m_count(other.m_count)
{
std::cout << "times_t::copy" << std::endl;
}
times_t(times_t && other)
: m_count(other.m_count)
{
std::cout << "times_t::move" << std::endl;
}
times_t & operator = (const times_t & other)
{
m_count = other.m_count;
std::cout << "times_t::assign" << std::endl;
return (*this);
}
times_t & operator ++ ()
{
++m_count;
return (*this);
}
int m_count;
};
std::ostream & operator << (std::ostream & os, const times_t & times)
{
return (os << times.m_count);
}
static bool choose_zero(times_t & times)
{
int random = std::rand() % 6;
if (0 == random)
{
std::cout << "times: " << ++times << ", ooh, you get 0 now!!!" << std::endl;
return (true);
}
else
{
std::cout << "times: " << ++times << ", no, you get " << random << ", try again!!!" << std::endl;
return (false);
}
}
static void sync_choose_zero()
{
times_t times;
std::cout << "sync_choose_zero start" << std::endl;
while (true)
{
if (choose_zero(times))
{
break;
}
}
std::cout << "sync_choose_zero end" << std::endl;
}
template <typename function_t, typename callback_t>
class async_operator
{
public:
typedef boost::asio::io_context ioc_t;
typedef function_t fn_t;
typedef callback_t cb_t;
typedef times_t ts_t;
public:
async_operator(ioc_t & ioc, fn_t f, cb_t c)
: m_ioc(ioc)
, m_fn(f)
, m_cb(c)
, m_ts()
{
std::cout << "async_operator::constructor()" << std::endl;
}
~async_operator()
{
std::cout << "async_operator::destructor()" << std::endl;
}
async_operator(const async_operator & other) // try " = default" or " = delete""
: m_ioc(other.m_ioc)
, m_fn(other.m_fn)
, m_cb(other.m_cb)
, m_ts(other.m_ts)
{
std::cout << "async_operator::copy" << std::endl;
}
async_operator(async_operator && other) // try " = default" or " = delete"
: m_ioc(other.m_ioc)
, m_fn(std::move(other.m_fn))
, m_cb(std::move(other.m_cb))
, m_ts(other.m_ts)
{
std::cout << "async_operator::move" << std::endl;
}
async_operator & operator = (const async_operator &) = delete;
public:
void operator () ();
private:
ioc_t & m_ioc;
fn_t m_fn;
cb_t m_cb;
ts_t m_ts;
};
template <typename function_t, typename callback_t>
void async_operator<function_t, callback_t>::operator () ()
{
if (m_fn(m_ts))
{
m_cb();
}
else
{
m_ioc.post(async_operator(std::move(*this))); // try use async_operator(*this) replace async_operator(std::move(*this))
}
}
static void show(const char * message)
{
std::cout << message << std::endl;
}
template <typename function_t, typename callback_t>
void async_choose_zero(boost::asio::io_context & ioc, function_t fn, callback_t cb)
{
std::cout << "async_choose_zero start" << std::endl;
ioc.post(async_operator<function_t, callback_t>(ioc, fn, cb));
std::cout << "async_choose_zero end" << std::endl;
}
int main(int, char *[])
{
std::srand(static_cast<unsigned int>(time(nullptr)));
boost::asio::io_context ioc;
std::cout << "call sync_choose_zero before:" << std::endl;
sync_choose_zero();
std::cout << "call sync_choose_zero after:" << std::endl;
std::cout << std::endl;
std::cout << "call async_choose_zero before:" << std::endl;
async_choose_zero(ioc, choose_zero, std::bind(show, "async_choose_zero has async done!!!"));
std::cout << "call async_choose_zero after:" << std::endl;
ioc.run();
return (0);
}