一个有趣的测试【std::bind、std::move、boost::asio::post()】

本文详细探讨了C++中std::bind的用法,如何通过它来绑定函数对象和参数。同时,解释了std::move在资源管理中的作用,特别是与智能指针配合的情况。此外,还介绍了boost::asio::post()在异步编程中的关键角色,如何利用它来调度任务到IO服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常忘记,留个代码给自己快速理解回忆

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值