boost asio 性能与线程安全性

本文深入探讨了Boost.Asio中并发编程的关键概念,包括如何使用Strands和IoService来实现高性能的多线程应用。重点解释了为什么在同一socket上不能同时读写,并详细介绍了三种不同配置的性能比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://stackoverflow.com/questions/12794107/why-do-i-need-strand-per-connection-when-using-boostasio/12801042

http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/overview/core/strands.html

url1:  同一个socket上, 不能 read write读写不能并发。。。。 !!!!???? 这个感觉不可能吧。。。那也太弱智了。。

====确实不能对同一个socket同时读写,socket能封装了basic_socket_stream, 该类的对象不能支持同时进行读写操作。ps, fstream也不能支持同时读写吧。。



http://stackoverflow.com/questions/4078484/using-boost-sockets-do-i-need-only-one-io-service

只强调, 不能 对同一个socket同时多读, 或者同时多写。



http://stackoverflow.com/questions/6161725/boostasio-threadpool-vs-io-service-per-cpu-design

性能递升。。。

  1. single thread and a single io_service
  2. multiple threads, each invoking io_service::run() from a single io_service. Use strands for handlers that require access to shared data structures.
  3. io_service per cpu

http://en.highscore.de/cpp/boost/asio.html#asio_networkprogramming

high score boost里面有boost asio的用法

1) single thread && single io_service, 最简单, 性能最一般

2) multithread && single io_service

3) io_service per thread.  multi io_service.

这三个性能应该也是递增的。。。



http://stackoverflow.com/questions/11010530/do-we-need-multiple-io-service-per-thread-for-threaded-boostasio-server-with/11011080#11011080

single thread && single io_service性能肯定是不够的。

boost asio example

HTTP Server 2

An HTTP server using an io_service-per-CPU design.

HTTP Server 3

An HTTP server using a single io_service and a thread pool calling io_service::run().



http://stackoverflow.com/questions/8557067/thread-safe-coroutines-with-asio
http://www.crystalclearsoftware.com/soc/coroutine/coroutine/coroutine_thread.html
coroutine能使异步切碎的逻辑看起来像同步的方式写代码。
但是coroutine不能支持多线程, 只能一个ioservice一个线程的方式。



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值