Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::mutex over a socket
From: Kulti (kultihell_at_[hidden])
Date: 2011-03-14 11:59:39


On Mon, Mar 14, 2011 at 3:28 PM, Marco Piacentini
<marcopiacenta_at_[hidden]> wrote:
> Hi guys...I've 2 thread and I would like that one of these performs a
> synchronous read, menawhile the other performs a synchrounous write...adn
> obviously they share the same socket.....the signature of the functions are
> the following:
>
> length=boost::asio::read(socket_,boost::asio::buffer(msg,sizeof(msg)), ec);
>
> boost::asio::write(socket_,boost::asio::buffer(msgToSend,sizeof(msgToSend)));
>
> socket_ is a private field of the classe Server.
>
> How can I use the mutex to perform concurrently and safetly these
> operations?
>
> Thanks..
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

The typical usage of mutex:
1. Add boost::mutex mutex_ as private field of class Server
2. When you read/write just add mutex_.lock() before and
mutex_.unlock() after. Also you can use boost::mutex::scoped_lock if
it's convenient.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net