使用异步操作时为什么没有堆栈溢出?(Why there is no stack overflow while using asynchronous operations?)
我正在使用Boost.Asio来学习异步操作。 在阅读了很多关于这个概念的文章之后,我仍然感到困惑的是为什么这段代码是Asio Docs的C ++ 11例子的一部分并没有使堆栈溢出? 我无法想象代码流在这个地方。 它看起来很递归,因为
do_accept();
一次又一次地召唤自己...我可以想象这个堆栈适用于20个客户端,但是对于2 000个客户端? 我认为对于异步操作,将acceptor_.async_accept()
置于循环中并且不在其中进行递归调用更为明确。 它会一样吗?我添加了类成员
call_nr
用于测试目的。代码:
class server { public: server(boost::asio::io_service& io_service, short port) : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), socket_(io_service), call_nr(0) { do_accept(); } private: void do_accept() { acceptor_.async_accept(socket_, [this](boost::system::error_code ec) // lambda equation { using namespace std; cout << "Call nr " << (++call_nr) << endl; if (!ec) { std::make_shared<session>(std::move(socket_))->start(); } do_accept(); }); } int call_nr; tcp::acceptor acceptor_; tcp::socket socket_; };
I'm using Boost.Asio for learning asynchronic operations. After reading many articles about this concept I'm still confused why this piece of code which is part of C++11 Examples from Asio Docs isn't making stack overflow? I just can't imagine the flow of the code in this place. It looks recursive so much, because
do_accept();
calls itself again and again... I can imagine that the stack works for 20 clients, but for 2 000 clients? I thought that for Asynchronous operations it is more clear to placeacceptor_.async_accept()
within a loop and without recursive call inside it. Would it work the same?Class member
call_nr
is added by me for testing purposes.The code:
class server { public: server(boost::asio::io_service& io_service, short port) : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), socket_(io_service), call_nr(0) { do_accept(); } private: void do_accept() { acceptor_.async_accept(socket_, [this](boost::system::error_code ec) // lambda equation { using namespace std; cout << "Call nr " << (++call_nr) << endl; if (!ec) { std::make_shared<session>(std::move(socket_))->start(); } do_accept(); }); } int call_nr; tcp::acceptor acceptor_; tcp::socket socket_; };
原文:https://stackoverflow.com/questions/35727482
最满意答案
您可以查看我撰写的关于如何拨打Google Directions API的博客文章指南解析这一点并将其呈现在Google地图上:
You could take a look at this blog post guide I wrote on how to make a call to Google directions API parse the point and present them on Google Map:
Google maps directions using Google Directions API and Polylines
相关问答
更多-
GoogleMaps使用自己的方式渲染折线(不是通过Polyline )。 有两种可能的方法来实现类似的东西: 1)使用常规的Polyline类并绘制2条折线 - 较厚的一条(比如说10px宽度)和更细的一条(8px)。 您将无法获得与GoogleMap应用程序相同的视觉效果,但您将更好地感知真实对象而不是纯折线 2)使用自定义TileOverlay ,因此您将负责渲染折线。 这种方法要复杂得多,但这是我必须在我的一个应用程序中实现的,因为折线的默认外观是不可接受的。 您可以查看map util lib以大 ...
-
由于我的代码中所述的原因,在保持良好性能的同时实现这是一件相当困难的事情。 捕捉到标记拖动标记是您要求的,但这样做有限制和问题。 我将为您提供代码来执行此操作,而无需在拖动时捕捉到道路,而是在标记拖动的END处。 请注意,取消注释下面的一行代码可以使其在您按要求拖动时捕捉到道路,但我建议不要出于下面所述的原因。 请在此处查看我的代码末尾的注释,并在此处查看http://jsfiddle.net/VvR5k/1/您可以使用上次位置的Location.distanceBetween到当前位置。 如果您希望距离开始和结束位置的总距离,请在您的位置发生变化时保持运行总计 you can use Location.distanceBetween on your last location to your current location. If you want a total distance from your start and end positions then keep a running total as y ...
Android Google Maps - 如何检查Polyline是否为空(Android Google Maps - how to check if Polyline is null)[2022-01-26]
从你的“coordList”中删除所有条目,并在你的PolyLine对象或mMap.clear() polyline.remove()上调用polyline.remove() mMap.clear()以删除所有标记,然后将新的LatLng值添加到坐标列表中。 制作这些类变量: PolyLine mPolyline; ArrayListmCoordList = new ArrayList (); GoogleMap mMap; 现在,您只需在需要时添加多段线即可: publi ... 如何让谷歌地图V3多段线从给定点捕捉道路?(How to make Google Maps V3 polyline snap to road from given points?)[2023-05-02]
如果你只是想要有路标的路线,你应该用这些路标调用一次directionsService ,像这样(未测试): var service = new google.maps.DirectionsService(); var directionsDisplay = new google.maps.DirectionsRenderer(); directionsDisplay.setMap(map); var waypts = []; for(j=1;j(Android)是否可以在谷歌地图中更改折线的形状?((Android) Is it possible to change the shape of the polyline in google maps?)[2023-10-24]
这会做什么? GoogleMap map; // Add a thick blue line Polyline line = map.addPolyline(new PolylineOptions() .add(new LatLng1, new LatLng2) .width(10) .color(Color.BLUE)); // Add a thin white line Polyline line = map.addPolyline(new PolylineOpt ...您可以查看我撰写的关于如何拨打Google Directions API的博客文章指南解析这一点并将其呈现在Google地图上: Google使用Google Directions API和Polylines映射路线 You could take a look at this blog post guide I wrote on how to make a call to Google directions API parse the point and present them on Google Map ...问题是Google Play服务版本8.3.0中的PolylineOptions实现不包含isClickable()方法。 它已在8.4.0版本中添加,如果您查看GitHub上的GeoJsonLineStringStyle.java 历史记录,您会发现它在24天前已更改以添加折线可点击性。 所以要解决您的问题,您需要更改build.gradle compile 'com.google.android.gms:play-services:8.3.0' 至 compile 'com.google.androi ...DirectionsService是异步的。 在使用它之前清除回调例程中的snap_path数组或创建2个单独的snap_path数组: function initialize() { var pos = new google.maps.LatLng(-26.431228,-69.572755); var myOptions = { zoom: 5, center: pos, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map ...在Google地图上绘制虚线折线 - Android [复制](Draw dotted polyline on Google Map - Android [duplicate])[2022-04-07]
截至2017年2月,Google在Google Maps Android API v2中发布了一系列针对折线和多边形的自定义。 现在,您可以创建虚线或点线折线。 请参阅“ 形状指南”中有关新功能的信息。 请参阅“折线和多边形”教程中的示例。 您还可以在此处阅读相应的博文: https://maps-apis.googleblog.com/2017/02/styling-and-custom-data-for-polylines.html As of February 2017, Google release ...相关文章
更多- AI八数码问题 Stack Overflow
- Stack Overflow Architecture Update - Now At 95 Million Page Views A Month
- Hadoop Operations
- Spring Data: a new perspective of data operations
- 一天一道算法题--6.7--stack
- java jxl 操作excel 时老是这个错误
- java操作Memcached
- 怎样解决内存溢出
- 记录一点零碎想法
- Java 数据结构
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。