{"id":19036164,"url":"https://github.com/tx7do/test_asio","last_synced_at":"2026-05-05T21:30:15.674Z","repository":{"id":64137033,"uuid":"494946564","full_name":"tx7do/test_asio","owner":"tx7do","description":null,"archived":false,"fork":false,"pushed_at":"2022-12-03T04:58:13.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-02T05:32:18.677Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tx7do.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-22T03:09:14.000Z","updated_at":"2022-05-22T04:51:12.000Z","dependencies_parsed_at":"2023-01-14T23:45:50.617Z","dependency_job_id":null,"html_url":"https://github.com/tx7do/test_asio","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Ftest_asio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Ftest_asio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Ftest_asio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Ftest_asio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tx7do","download_url":"https://codeload.github.com/tx7do/test_asio/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240093142,"owners_count":19746774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:53:38.661Z","updated_at":"2026-05-05T21:30:15.373Z","avatar_url":"https://github.com/tx7do.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Asio测试程序\r\n\r\n项目运行于WSL Ubuntu之下。\r\n\r\n* Asio 的Linux Epoll实现，使用的是水平触发模式（Level Trigger）。\r\n\r\n1. 单context，单thread；\r\n2. 单context，多thread；\r\n3. 多context，多thread（一对一）。\r\n\r\ncpprest采用的是单context，多thread。  \r\nMongoDB采用的是：acceptor一个context一个thread；每个连接也是一个context一个thread。\r\n\r\n## Ubuntu安装依赖项\r\n\r\n```bash\r\n# 开发必要软件包，包含：dpkg-dev fakeroot g++ g++-4.6 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libdpkg-perl libstdc++6-4.6-dev libtimedate-perl\r\nsudo apt-get install build-essential\r\n\r\n# build tools\r\nsudo apt-get install cmake gcc clang gdb llvm lldb llvm-dev liblldb-dev\r\n\r\n# Boost.Asio\r\nsudo apt-get install libboost-dev\r\n\r\n# Asio only\r\nsudo apt-get install -y libasio-dev\r\n\r\n# Protocol Buffers\r\nsudo apt-get install libprotobuf-dev\r\n\r\n# protoc\r\nsudo apt install protobuf-compiler\r\n```\r\n\r\n## post和dispatch\r\n\r\n首先以伪代码描述其功能：\r\n\r\n```c++\r\nvoid post(Handler handler)\r\n{\r\n  _queue.push(handler);\r\n}\r\n\r\nvoid dispatch(Handler handler)\r\n{\r\n  if (can_execute())\r\n    handler();\r\n  else\r\n    post(handler);\r\n}\r\n\r\nvoid run()\r\n{\r\n\t_work_thrd_id = boost::this_thread::get_id();\r\n\twhile (!_queue.empty())\r\n    {\r\n        Handler handler = _queue.front();\r\n        _queue.pop();\r\n        handler();\r\n    }\r\n}\r\n\r\nbool can_execute()\r\n{\r\n  return _work_thrd_id == boost::this_thread::get_id();\r\n}\r\n```\r\n\r\n1. post永远都不会直接调用handler，而是将handler放入队列中，等待执行。\r\n2. dispatch则是判断是否可以直接执行handler，如果可以执行，则直接执行，否则将handler放入队列中等待执行。\r\n3. run是一个循环，每次循环都会从队列中取出一个handler，然后执行。\r\n\r\n## strand\r\n\r\n类boost::asio::io_context::strand的主要作用是在asio中利用多线程进行事件处理的时候，如果涉及到多线程访问共享资源，借助于strand类，我们不需要显示的使用线程同步相关的类（比如mutex）就可以让多个事件处理函数依次执行。 简而言之，strand定义了事件处理程序的严格顺序调用。\r\n\r\n## 参考文档\r\n\r\n* [asio的异步与线程模型解析](https://www.cnblogs.com/ishen/p/14593598.html)\r\n* [浅谈 Boost.Asio 的多线程模型](http://senlinzhan.github.io/2017/09/17/boost-asio/)\r\n* [Boost.Asio基本原理](https://mmoaay.gitbooks.io/boost-asio-cpp-network-programming-chinese/content/Chapter2.html)\r\n* [Asio实现浅析](https://zhuanlan.zhihu.com/p/55503053)\r\n* [A Priority Queue with Boost.Asio](https://zhuanlan.zhihu.com/p/87400227)\r\n* [Task Execution with Asio](https://www.packt.com/task-execution-asio/)\r\n* [Event Loop](https://gist.github.com/kassane/f2330ef44b070f4a5fa9d59c770f68e9)\r\n* [Boost asio io_service dispatch vs post](https://stackoverflow.com/questions/2326588/boost-asio-io-service-dispatch-vs-post)\r\n* [To post or to dispatch?](http://thisthread.blogspot.com/2011/06/to-post-or-to-dispatch.html)\r\n* [Multithreaded execution with asio, part 1](https://dens.website/tutorials/cpp-asio/multithreading)\r\n* [Multithreaded execution with asio, part 2](https://dens.website/tutorials/cpp-asio/multithreading-2)\r\n* [【翻译】为何我们要使用boost strands](https://www.jianshu.com/p/70286c2ab544)\r\n* [Strands](https://zhuanlan.zhihu.com/p/87388918)\r\n* [How strands work and why you should use them](http://www.crazygaze.com/blog/2016/03/17/how-strands-work-and-why-you-should-use-them/)\r\n* [【翻译】为何我们要使用boost strands](https://www.crazygaze.com/blog/2016/03/17/how-strands-work-and-why-you-should-use-them/)\r\n* [How strands work and why you should use them](https://www.jianshu.com/p/70286c2ab544)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftx7do%2Ftest_asio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftx7do%2Ftest_asio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftx7do%2Ftest_asio/lists"}