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..
On Mon, Mar 14, 2011 at 3:28 PM, Marco Piacentini <marcopiacenta@yahoo.it> 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@lists.boost.org 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.
Ok...I've done something like this: void executeThread() { m_mutex.lock(); length=boost::asio::read(socket_,boost::asio::buffer(msg,sizeof(msg)), ec); m_mutex.unlock(); m_mutex.lock(); boost::asio::write(socket_,boost::asio::buffer(msgToSend,sizeof(msgToSend))); m_mutex.unlock(); } and I call two thread inside a member's function Server Class: void handle_accept() { .... .... if(fork()==0) { boost::thread thread_read(&server::executeThread,this); boost::thread thread_write(&server::executeThread,this); thread_read.join(); thread_write.join(); } else { .... .... } } Can I obtain in this way a thread that read and the other that write? and to obtain a ciclyc behaviour, where I have to put the for(;;)?thanks.. ________________________________ Da: Kulti <kultihell@gmail.com> A: boost-users@lists.boost.org Inviato: Lun 14 marzo 2011, 16:59:39 Oggetto: Re: [Boost-users] boost::mutex over a socket On Mon, Mar 14, 2011 at 3:28 PM, Marco Piacentini <marcopiacenta@yahoo.it> 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@lists.boost.org 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Mon, Mar 14, 2011 at 4:24 PM, Marco Piacentini <marcopiacenta@yahoo.it> wrote:
Ok...I've done something like this:
void executeThread() { m_mutex.lock(); length=boost::asio::read(socket_,boost::asio::buffer(msg,sizeof(msg)), ec); m_mutex.unlock();
m_mutex.lock(); boost::asio::write(socket_,boost::asio::buffer(msgToSend,sizeof(msgToSend))); m_mutex.unlock(); }
and I call two thread inside a member's function Server Class:
void handle_accept() { .... ....
if(fork()==0) { boost::thread thread_read(&server::executeThread,this); boost::thread thread_write(&server::executeThread,this); thread_read.join(); thread_write.join(); } else { .... .... }
}
Can I obtain in this way a thread that read and the other that write? and to obtain a ciclyc behaviour, where I have to put the for(;;)?thanks..
________________________________ Da: Kulti <kultihell@gmail.com> A: boost-users@lists.boost.org Inviato: Lun 14 marzo 2011, 16:59:39 Oggetto: Re: [Boost-users] boost::mutex over a socket
On Mon, Mar 14, 2011 at 3:28 PM, Marco Piacentini <marcopiacenta@yahoo.it> 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@lists.boost.org 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
At first, I cannot understand why you try to use one handler for different threads? If you wanna get two threads (one for read and one for write) you need two different handlers. At second, If you do fork - you get two processes, which cannot share mutex. If you need to interprocess synchronization you should use other mechanisms, e.g. lock-file.
Ok I know that two processes don't share mutex...but I must use the fork(user need) only to create a parent process that listen new connection,while the child server the request. After for each request I would have one thread that listen on the socket and the other that write on it. For the second question...you're right, maybe I have to use two different handlers...but if I'll do this, where I put the mutex? .... boost::thread thread_read(&server::executeThreadRead,this); boost::thread thread_write(&server::executeThreadWrite,this); thread_read.join(); thread_write.join(); ... void executeThreadRead() { m_mutex.lock(); length=boost::asio::read(socket_,boost::asio::buffer(msg,sizeof(msg)), ec); m_mutex.unlock(); } void executeThreadWrite() { m_mutex.lock(); boost::asio::write(socket_,boost::asio::buffer(msgToSend,sizeof(msgToSend))); m_mutex.unlock(); } because in this way the mutex "protected" the calls read and write, but not the shared socket...isnt'it? ________________________________ Da: Kulti <kultihell@gmail.com> A: boost-users@lists.boost.org Inviato: Lun 14 marzo 2011, 17:44:02 Oggetto: Re: [Boost-users] boost::mutex over a socket On Mon, Mar 14, 2011 at 4:24 PM, Marco Piacentini <marcopiacenta@yahoo.it> wrote:
Ok...I've done something like this:
void executeThread() { m_mutex.lock(); length=boost::asio::read(socket_,boost::asio::buffer(msg,sizeof(msg)), ec); m_mutex.unlock();
m_mutex.lock(); boost::asio::write(socket_,boost::asio::buffer(msgToSend,sizeof(msgToSend))); m_mutex.unlock(); }
and I call two thread inside a member's function Server Class:
void handle_accept() { .... ....
if(fork()==0) { boost::thread thread_read(&server::executeThread,this); boost::thread thread_write(&server::executeThread,this); thread_read.join(); thread_write.join(); } else { .... .... }
}
Can I obtain in this way a thread that read and the other that write? and to obtain a ciclyc behaviour, where I have to put the for(;;)?thanks..
________________________________ Da: Kulti <kultihell@gmail.com> A: boost-users@lists.boost.org Inviato: Lun 14 marzo 2011, 16:59:39 Oggetto: Re: [Boost-users] boost::mutex over a socket
On Mon, Mar 14, 2011 at 3:28 PM, Marco Piacentini <marcopiacenta@yahoo.it> 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@lists.boost.org 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
At first, I cannot understand why you try to use one handler for different threads? If you wanna get two threads (one for read and one for write) you need two different handlers. At second, If you do fork - you get two processes, which cannot share mutex. If you need to interprocess synchronization you should use other mechanisms, e.g. lock-file. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Kulti
-
Marco Piacentini