KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
( This is a simplified version of my original question ) I have several threads that write to a boost asio socket. This seems to work very well, with no problems. The documentation says a shared socket is not thread safe( here , way down at the bottom ) so I am wondering if I should protect the socket with mutex, or something. This question insists that protection is necessary, but gives no advice on how to do so. All the answers to my original question also insisted that what I was doing dangerous, and most urged me to replace my writes with async_writes or even more complicated things. However, I am reluctant to do this, since it would complicate code that is already working and none of the answerers convinced me they knew what they ware talking about - they seemed to have read the same documentation as I and were guessing, just as I was. So, I wrote a simple program to stress test writing to a shared socket from two threads. Here is the server, which simply writes out whatever it receives from the client int main() { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3001)); tcp::socket socket(io_service); acceptor.accept(socket); for (;;) { char mybuffer[1256]; int len = socket.read_some(boost::asio::buffer(mybuffer,1256)); mybuffer[len] = '\0'; std::cout << mybuffer; std::cout.flush(); } return 0; } Here is the client, which creates two threads that write to a shared socket as fast as they can boost::asio::ip::tcp::socket * psocket; void speaker1() { string msg("speaker1: hello, ser
Tags (comma-separated)
Save Edits
Cancel