Discussion:
[asio-users] strand and sync call
he keding
2012-10-20 05:00:45 UTC
Permalink
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?
}
...
}
Marat Abrarov
2012-10-20 06:44:32 UTC
Permalink
but if i want a member function must have a return value(sync call), how to avoid mutex.
Callback or future are the common solutions.

Look at socket::async_read_some: destination for the result has to be provided by caller and is passed to async member
function by reference, the lifetime of result's destination has to be guaranteed by caller and can be done via callback
(shared_ptr as one of the callback's members).

Future can be implemented by means of callback as a wrapper containing own condition_variable/mutex (for safe result
copying/readiness signaling).

Regards,
Marat Abrarov.
he keding
2012-10-20 07:11:28 UTC
Permalink
Thanks abrarov,
I know callback as async result, but I think this increase complex for some
very simple function.
for exampe:

class Obj
{
public:
int get_a()
{
return a;
}

private:
int a;
};

If i use Obj by strand in multithread, I should code like this

class Obj
{
public:
void get_a(Callback& cb)
{
m_strand.post(boost::bind(&Connection::handle_get_a,
shared_from_this(), cb));
}

private:
void handle_get_a(Callback& cb)
{
cb(a);
}

int a;
};
and the caller also move next code to callback handle.

you see, it's much complex to simple return a, and also async callback obj
take more memory.

so, is mutex better for this condition which a class has many sync member
function or more reader than writer?
Post by he keding
Post by he keding
but if i want a member function must have a return value(sync call), how
to avoid mutex.
Callback or future are the common solutions.
Look at socket::async_read_some: destination for the result has to be
provided by caller and is passed to async member
function by reference, the lifetime of result's destination has to be
guaranteed by caller and can be done via callback
(shared_ptr as one of the callback's members).
Future can be implemented by means of callback as a wrapper containing own
condition_variable/mutex (for safe result
copying/readiness signaling).
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Marat Abrarov
2012-10-20 09:18:04 UTC
Permalink
I know callback as async result, but I think this increase complex for some very simple function.
IMHO you may be interested in some home-grown future (with same interface as std::future has) built on the top of async
Post by Marat Abrarov
Future can be implemented by means of callback as a wrapper containing own condition_variable/mutex (for safe
result copying/readiness signaling).
so, is mutex better for this condition which a class has many sync member function or more reader than writer?
In asio-samples (http://sourceforge.net/projects/asio-samples/) ma::echo::server::session_manager class uses something
like this
(http://asio-samples.svn.sourceforge.net/viewvc/asio-samples/trunk/include/ma/echo/server/session_manager.hpp?revision=7
18&view=markup) - distinct stats_collector_ member with mutex (within it) for safe access to the data shared between
sync and async methods of session_manager.

Regards,
Marat Abrarov.
he keding
2012-10-20 12:52:16 UTC
Permalink
ok, I'll take a look, thanks :)
Post by he keding
I know callback as async result, but I think this increase complex for
some very simple function.
IMHO you may be interested in some home-grown future (with same interface
as std::future has) built on the top of async
Post by he keding
Post by Marat Abrarov
Future can be implemented by means of callback as a wrapper containing
own condition_variable/mutex (for safe
Post by he keding
Post by Marat Abrarov
result copying/readiness signaling).
so, is mutex better for this condition which a class has many sync
member function or more reader than writer?
In asio-samples (http://sourceforge.net/projects/asio-samples/)
ma::echo::server::session_manager class uses something
like this
(
http://asio-samples.svn.sourceforge.net/viewvc/asio-samples/trunk/include/ma/echo/server/session_manager.hpp?revision=7
18&view=markup) - distinct stats_collector_ member with mutex (within it)
for safe access to the data shared between
sync and async methods of session_manager.
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Loading...