我可以在执行读写的2个线程之间共享boost :: asio :: tcp :: socket对象 [英] Can I share boost::asio::tcp::socket object between 2 threads that perform read and write

查看:249
本文介绍了我可以在执行读写的2个线程之间共享boost :: asio :: tcp :: socket对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个线程,一个通过TCP套接字发送,另一个通过TCP套接字接收数据. 我使用boost::asio::read()boost::asio::write()进行读写.我的问题是在读写操作期间是否必须保护对套接字对象的访问?

I have two threads, one sending and another receiving data via TCP socket. I use boost::asio::read() and boost::asio::write() for reading and writing. My question is do I have to guard the access to socket object during read and write operation?

另一种情况是,如果我有两个线程都使用同一个套接字对象进行写?

The other case would be what if I have two threads both writing using the same socket object?

推荐答案

当在两个或多个线程之间共享时,套接字不是线程安全的.有关更多信息,请参见Boost.Asio 文档.

The socket is not thread safe when shared between two or more threads. For more information look at the Boost.Asio documentation.

这篇关于我可以在执行读写的2个线程之间共享boost :: asio :: tcp :: socket对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆