he keding
2012-10-20 05:00:45 UTC
hi all,
there's a class like this
class Connection
{
public:
void start()
{
m_strand.post(boost::bind(&Connection::handle_start,
shared_from_this()));
}
void send(const msgbuf_ptr& mb)
{
m_strand.post(boost::bind(&Connection::handle_send,
shared_from_this(), mb));
}
void close()
{
m_strand.post(boost::bind(&Connection::handle_close,
shared_from_this()));
}
...
private:
boost::asio::io_service::strand m_strand;
};
the class use strand to wrap public member function to avoid mutex.
but if i want a member function must have a return value(sync call), how to
avoid mutex.
for example:
class Connection
{
public:
std::string remote_addr()
{
/*
if i use strand post, it's a async call, can't return addr result
if i use mutex, i must use mutex in all public member function, so
strand is not use any more
how to solve this problem?
}
...
}
there's a class like this
class Connection
{
public:
void start()
{
m_strand.post(boost::bind(&Connection::handle_start,
shared_from_this()));
}
void send(const msgbuf_ptr& mb)
{
m_strand.post(boost::bind(&Connection::handle_send,
shared_from_this(), mb));
}
void close()
{
m_strand.post(boost::bind(&Connection::handle_close,
shared_from_this()));
}
...
private:
boost::asio::io_service::strand m_strand;
};
the class use strand to wrap public member function to avoid mutex.
but if i want a member function must have a return value(sync call), how to
avoid mutex.
for example:
class Connection
{
public:
std::string remote_addr()
{
/*
if i use strand post, it's a async call, can't return addr result
if i use mutex, i must use mutex in all public member function, so
strand is not use any more
how to solve this problem?
}
...
}