{"id":1422,"date":"2018-06-20T20:03:45","date_gmt":"2018-06-20T12:03:45","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1422"},"modified":"2018-06-20T20:03:45","modified_gmt":"2018-06-20T12:03:45","slug":"boost-interprocess-%e8%bf%9b%e7%a8%8b%e9%97%b4%e9%80%9a%e8%ae%af","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1422","title":{"rendered":"\u00a0Boost.Interprocess\u00a0\u8fdb\u7a0b\u95f4\u901a\u8baf"},"content":{"rendered":"<p class=\"title\">Boost C++ \u5e93<\/p>\n<form class=\"toc\" action=\"http:\/\/zh.highscore.de\/cpp\/boost\/interprocesscommunication.html\"><select size=\"1\" onchange=\"location.href=options[selectedIndex].value\"><option value=\"frontpage.html\">\u5bfc\u8a00<\/option><option value=\"introduction.html\">\u7b2c\u00a01\u00a0\u7ae0\u00a0\u7b80\u4ecb<\/option><option value=\"smartpointers.html\">\u7b2c\u00a02\u00a0\u7ae0\u00a0\u667a\u80fd\u6307\u9488<\/option><option value=\"functionobjects.html\">\u7b2c\u00a03\u00a0\u7ae0\u00a0\u51fd\u6570\u5bf9\u8c61<\/option><option value=\"eventhandling.html\">\u7b2c\u00a04\u00a0\u7ae0\u00a0\u4e8b\u4ef6\u5904\u7406<\/option><option value=\"stringhandling.html\">\u7b2c\u00a05\u00a0\u7ae0\u00a0\u5b57\u7b26\u4e32\u5904\u7406<\/option><option value=\"multithreading.html\">\u7b2c\u00a06\u00a0\u7ae0\u00a0\u591a\u7ebf\u7a0b<\/option><option value=\"asio.html\">\u7b2c\u00a07\u00a0\u7ae0\u00a0\u5f02\u6b65\u8f93\u5165\u8f93\u51fa<\/option><option selected=\"selected\" value=\"interprocesscommunication.html\">\u7b2c\u00a08\u00a0\u7ae0\u00a0\u8fdb\u7a0b\u95f4\u901a\u8baf<\/option><option value=\"filesystem.html\">\u7b2c\u00a09\u00a0\u7ae0\u00a0\u6587\u4ef6\u7cfb\u7edf<\/option><option value=\"datetime.html\">\u7b2c\u00a010\u00a0\u7ae0\u00a0\u65e5\u671f\u4e0e\u65f6\u95f4<\/option><option value=\"serialization.html\">\u7b2c\u00a011\u00a0\u7ae0\u00a0\u5e8f\u5217\u5316<\/option><option value=\"parser.html\">\u7b2c\u00a012\u00a0\u7ae0\u00a0\u8bcd\u6cd5\u5206\u6790\u5668<\/option><option value=\"containers.html\">\u7b2c\u00a013\u00a0\u7ae0\u00a0\u5bb9\u5668<\/option><option value=\"datastructures.html\">\u7b2c\u00a014\u00a0\u7ae0\u00a0\u6570\u636e\u7ed3\u6784<\/option><option value=\"errorhandling.html\">\u7b2c\u00a015\u00a0\u7ae0\u00a0\u9519\u8bef\u5904\u7406<\/option><option value=\"castoperators.html\">\u7b2c\u00a016\u00a0\u7ae0\u00a0\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u7b26<\/option><\/select><\/form>\n<hr class=\"hrhead\" \/>\n<h1 class=\"title\"><a name=\"interprocesscommunication\"><\/a>\u7b2c\u00a08\u00a0\u7ae0\u00a0\u8fdb\u7a0b\u95f4\u901a\u8baf<\/h1>\n<hr \/>\n<div class=\"toc\">\n<h3>\u76ee\u5f55<\/h3>\n<ul>\n<li><span class=\"sect1\"><a href=\"http:\/\/zh.highscore.de\/cpp\/boost\/interprocesscommunication.html#interprocesscommunication_general\">8.1 \u6982\u8ff0<\/a><\/span><\/li>\n<li><span class=\"sect1\"><a href=\"http:\/\/zh.highscore.de\/cpp\/boost\/interprocesscommunication.html#interprocesscommunication_shared_memory\">8.2 \u5171\u4eab\u5185\u5b58<\/a><\/span><\/li>\n<li><span class=\"sect1\"><a href=\"http:\/\/zh.highscore.de\/cpp\/boost\/interprocesscommunication.html#interprocesscommunication_managed_shared_memory\">8.3 \u6258\u7ba1\u5171\u4eab\u5185\u5b58<\/a><\/span><\/li>\n<li><span class=\"sect1\"><a href=\"http:\/\/zh.highscore.de\/cpp\/boost\/interprocesscommunication.html#interprocesscommunication_synchronization\">8.4 \u540c\u6b65<\/a><\/span><\/li>\n<li><span class=\"sect1\"><a href=\"http:\/\/zh.highscore.de\/cpp\/boost\/interprocesscommunication.html#interprocesscommunication_exercises\">8.5 \u7ec3\u4e60<\/a><\/span><\/li>\n<\/ul>\n<\/div>\n<p class=\"license\"><a href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/de\/deed.zh\" target=\"_top\" rel=\"license\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/zh.highscore.de\/cpp\/boost\/img\/88x31_cc_logo.gif\" alt=\"\" width=\"88\" height=\"31\" \/><\/a>\u00a0\u8be5\u4e66\u91c7\u7528\u00a0<a href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/de\/deed.zh\" target=\"_top\" rel=\"license\">Creative Commons License<\/a>\u00a0\u6388\u6743<\/p>\n<hr \/>\n<h2 class=\"title\"><a name=\"interprocesscommunication_general\"><\/a>8.1.\u00a0\u6982\u8ff0<\/h2>\n<div class=\"sect1\">\n<p>\u8fdb\u7a0b\u95f4\u901a\u8baf\u63cf\u8ff0\u7684\u662f\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u7684\u4e0d\u540c\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u673a\u5236\u3002 \u4f46\u4e0d\u5305\u62ec\u7f51\u7edc\u901a\u8baf\u65b9\u5f0f\u3002 \u5982\u679c\u9700\u8981\u7ecf\u7531\u7f51\u7edc\uff0c\u5728\u5f7c\u6b64\u8fd0\u884c\u5728\u4e0d\u540c\u8ba1\u7b97\u673a\u4e0a\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\uff0c\u8bf7\u770b<a class=\"xref\" title=\"\u7b2c\u00a07\u00a0\u7ae0\u00a0\u5f02\u6b65\u8f93\u5165\u8f93\u51fa\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/asio.html\">\u7b2c\u00a07\u00a0\u7ae0\u00a0<i>\u5f02\u6b65\u8f93\u5165\u8f93\u51fa<\/i><\/a>\uff0c\u8be5\u7ae0\u8bb2\u8ff0\u4e86 Boost.Asio \u5e93\u3002<\/p>\n<p>\u672c\u7ae0\u5c55\u793a\u4e86\u00a0<a class=\"link\" href=\"http:\/\/www.boost.org\/libs\/interprocess\/\">Boost.Interprocess<\/a>\u00a0\u5e93\uff0c\u5b83\u5305\u62ec\u4f17\u591a\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u63d0\u4f9b\u4e86\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u8fdb\u7a0b\u95f4\u901a\u8baf\u63a5\u53e3\u7684\u62bd\u8c61\u5c42\u3002 \u867d\u7136\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u8fdb\u7a0b\u95f4\u901a\u8baf\u6982\u5ff5\u975e\u5e38\u76f8\u8fd1\uff0c\u4f46\u63a5\u53e3\u7684\u53d8\u5316\u5374\u5f88\u5927\u3002 Boost.Interprocess \u5e93\u4f7f\u901a\u8fc7C++\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u6210\u4e3a\u53ef\u80fd\u3002<\/p>\n<p>\u867d\u7136 Boost.Asio \u4e5f\u53ef\u4ee5\u7528\u6765\u5728\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u7684\u5e94\u7528\u7a0b\u5e8f\u95f4\u4ea4\u6362\u6570\u636e\uff0c\u4f46\u662f\u4f7f\u7528 Boost.Interprocess \u5e93\u901a\u5e38\u6027\u80fd\u66f4\u597d\u3002 Boost.Interprocess \u5e93\u5b9e\u9645\u4e0a\u662f\u4f7f\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u529f\u80fd\u4f18\u5316\u4e86\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u6570\u636e\u4ea4\u6362\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u662f\u4efb\u4f55\u4e0d\u9700\u8981\u7f51\u7edc\u65f6\u5e94\u7528\u7a0b\u5e8f\u95f4\u6570\u636e\u4ea4\u6362\u7684\u9996\u9009\u3002<\/p>\n<\/div>\n<hr \/>\n<h2 class=\"title\"><a name=\"interprocesscommunication_shared_memory\"><\/a>8.2.\u00a0\u5171\u4eab\u5185\u5b58<\/h2>\n<div class=\"sect1\">\n<p>\u5171\u4eab\u5185\u5b58\u901a\u5e38\u662f\u8fdb\u7a0b\u95f4\u901a\u8baf\u6700\u5feb\u7684\u5f62\u5f0f\u3002 \u5b83\u63d0\u4f9b\u4e00\u5757\u5728\u5e94\u7528\u7a0b\u5e8f\u95f4\u5171\u4eab\u7684\u5185\u5b58\u533a\u57df\u3002 \u4e00\u4e2a\u5e94\u7528\u80fd\u591f\u5728\u53e6\u4e00\u4e2a\u5e94\u7528\u8bfb\u53d6\u6570\u636e\u65f6\u5199\u6570\u636e\u3002<\/p>\n<p>\u8fd9\u6837\u4e00\u5757\u5185\u5b58\u533a\u7528 Boost.Interprocess \u7684\u00a0<code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u7c7b\u8868\u793a\u3002 \u4e3a\u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u9700\u8981\u5305\u542b\u00a0<code class=\"filename\">boost\/interprocess\/shared_memory_object.hpp<\/code>\u00a0\u5934\u6587\u4ef6\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/shared_memory_object.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, \"Highscore\", boost::interprocess::read_write); \r\n  shdmem.truncate(1024); \r\n  std::cout &lt;&lt; shdmem.get_name() &lt;&lt; std::endl; \r\n  boost::interprocess::offset_t size; \r\n  if (shdmem.get_size(size)) \r\n    std::cout &lt;&lt; size &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.2.1\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p><code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4e09\u4e2a\u53c2\u6570\u3002 \u7b2c\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u5171\u4eab\u5185\u5b58\u662f\u8981\u521b\u5efa\u6216\u6253\u5f00\u3002 \u4e0a\u9762\u7684\u4f8b\u5b50\u5b9e\u9645\u4e0a\u662f\u6307\u5b9a\u4e86\u4e24\u79cd\u65b9\u5f0f\uff1a\u7528\u00a0<code class=\"code\">boost::interprocess::open_or_create<\/code>\u00a0\u4f5c\u4e3a\u53c2\u6570\uff0c\u5171\u4eab\u5185\u5b58\u5982\u679c\u5b58\u5728\u5c31\u5c06\u5176\u6253\u5f00\uff0c\u5426\u5219\u521b\u5efa\u4e4b\u3002<\/p>\n<p>\u5047\u8bbe\u4e4b\u524d\u5df2\u7ecf\u521b\u5efa\u4e86\u5171\u4eab\u5185\u5b58\uff0c\u73b0\u6253\u5f00\u524d\u9762\u5df2\u7ecf\u521b\u5efa\u7684\u5171\u4eab\u5185\u5b58\u3002 \u4e3a\u4e86\u552f\u4e00\u6807\u8bc6\u4e00\u5757\u5171\u4eab\u5185\u5b58\uff0c\u9700\u8981\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u540d\u79f0\uff0c\u4f20\u9012\u7ed9\u00a0<code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u4e86\u8fd9\u4e2a\u540d\u79f0\u3002<\/p>\n<p>\u7b2c\u4e09\u4e2a\uff0c\u4e5f\u5c31\u662f\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u6307\u793a\u5e94\u7528\u7a0b\u5e8f\u5982\u4f55\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u3002 \u4f8b\u5b50\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u8bfb\u5199\u5171\u4eab\u5185\u5b58\uff0c\u8fd9\u662f\u56e0\u4e3a\u6700\u540e\u7684\u4e00\u4e2a\u53c2\u6570\u662f\u00a0<code class=\"code\">boost::interprocess::read_write<\/code>\u3002<\/p>\n<p>\u5728\u521b\u5efa\u4e00\u4e2a\u00a0<code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u7c7b\u578b\u7684\u5bf9\u8c61\u540e\uff0c\u76f8\u5e94\u7684\u5171\u4eab\u5185\u5b58\u5c31\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5efa\u7acb\u4e86\u3002 \u53ef\u662f\u6b64\u5171\u4eab\u5185\u5b58\u533a\u57df\u7684\u5927\u5c0f\u88ab\u521d\u59cb\u5316\u4e3a0.\u4e3a\u4e86\u4f7f\u7528\u8fd9\u5757\u533a\u57df\uff0c\u9700\u8981\u8c03\u7528\u00a0<code class=\"methodname\">truncate()<\/code>\u00a0\u51fd\u6570\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u4f20\u9012\u8bf7\u6c42\u7684\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u3002 \u5bf9\u4e8e\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5171\u4eab\u5185\u5b58\u63d0\u4f9b\u4e861,024\u5b57\u8282\u7684\u7a7a\u95f4\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c<code class=\"methodname\">truncate()<\/code>\u00a0\u51fd\u6570\u53ea\u80fd\u5728\u5171\u4eab\u5185\u5b58\u4ee5\u00a0<code class=\"code\">boost::interprocess::read_write<\/code>\u00a0\u65b9\u5f0f\u6253\u5f00\u65f6\u8c03\u7528\u3002 \u5982\u679c\u4e0d\u662f\u4ee5\u6b64\u65b9\u5f0f\u6253\u5f00\uff0c\u5c06\u629b\u51fa\u00a0<code class=\"exceptionname\">boost::interprocess::interprocess_exception<\/code>\u00a0\u5f02\u5e38\u3002<\/p>\n<p>\u4e3a\u4e86\u8c03\u6574\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\uff0c<code class=\"methodname\">truncate()<\/code>\u00a0\u51fd\u6570\u53ef\u4ee5\u88ab\u91cd\u590d\u8c03\u7528\u3002<\/p>\n<p>\u5728\u521b\u5efa\u5171\u4eab\u5185\u5b58\u540e\uff0c<code class=\"methodname\">get_name()<\/code>\u00a0\u548c\u00a0<code class=\"methodname\">get_size()<\/code>\u00a0\u51fd\u6570\u53ef\u4ee5\u5206\u522b\u7528\u6765\u67e5\u8be2\u5171\u4eab\u5185\u5b58\u7684\u540d\u79f0\u548c\u5927\u5c0f\u3002<\/p>\n<p>\u7531\u4e8e\u5171\u4eab\u5185\u5b58\u88ab\u7528\u4e8e\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\uff0c\u6240\u4ee5\u6bcf\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u6620\u5c04\u5171\u4eab\u5185\u5b58\u5230\u81ea\u5df1\u7684\u5730\u5740\u7a7a\u95f4\u4e0a\uff0c\u8fd9\u662f\u901a\u8fc7\u00a0<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u7c7b\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u4f60\u4e5f\u8bb8\u6709\u4e9b\u5947\u602a\uff0c\u4e3a\u4e86\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\uff0c\u8981\u4f7f\u7528\u4e24\u4e2a\u7c7b\u3002 \u662f\u7684\uff0c<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u8fd8\u80fd\u6620\u5c04\u4e0d\u540c\u7684\u5bf9\u8c61\u5230\u5177\u4f53\u5e94\u7528\u7684\u5730\u5740\u7a7a\u95f4\u3002 \u5982 Boost.Interprocess \u63d0\u4f9b\u00a0<code class=\"classname\">boost::interprocess::file_mapping<\/code>\u00a0\u7c7b\u5b9e\u9645\u4e0a\u4ee3\u8868\u7279\u5b9a\u6587\u4ef6\u7684\u5171\u4eab\u5185\u5b58\u3002 \u6240\u4ee5\u00a0<code class=\"classname\">boost::interprocess::file_mapping<\/code>\u00a0\u7c7b\u578b\u7684\u5bf9\u8c61\u5bf9\u5e94\u4e00\u4e2a\u6587\u4ef6\u3002 \u5411\u8fd9\u4e2a\u5bf9\u8c61\u5199\u5165\u7684\u6570\u636e\u5c06\u81ea\u52a8\u4fdd\u5b58\u5173\u8054\u7684\u7269\u7406\u6587\u4ef6\u4e0a\u3002 \u7531\u4e8e\u00a0<code class=\"classname\">boost::interprocess::file_mapping<\/code>\u00a0\u4e0d\u5fc5\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\uff0c\u4f46\u5374\u53ef\u4ee5\u4f7f\u7528\u00a0<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u5c06\u4efb\u610f\u90e8\u5206\u6620\u5c04\u5230\u5730\u5740\u7a7a\u95f4\uff0c\u6240\u4ee5\u5c31\u80fd\u5904\u7406\u51e0\u4e2aGB\u7684\u6587\u4ef6\uff0c\u800c\u8fd9\u4e2a\u6587\u4ef6\u572832\u4f4d\u7cfb\u7edf\u4e0a\u662f\u4e0d\u80fd\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e0a\u7684\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/shared_memory_object.hpp&gt; \r\n#include &lt;boost\/interprocess\/mapped_region.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, \"Highscore\", boost::interprocess::read_write); \r\n  shdmem.truncate(1024); \r\n  boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write); \r\n  std::cout &lt;&lt; std::hex &lt;&lt; \"0x\" &lt;&lt; region.get_address() &lt;&lt; std::endl; \r\n  std::cout &lt;&lt; std::dec &lt;&lt; region.get_size() &lt;&lt; std::endl; \r\n  boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only); \r\n  std::cout &lt;&lt; std::hex &lt;&lt; \"0x\" &lt;&lt; region2.get_address() &lt;&lt; std::endl; \r\n  std::cout &lt;&lt; std::dec &lt;&lt; region2.get_size() &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.2.2\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u4f7f\u7528\u00a0<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u7c7b\uff0c\u9700\u8981\u5305\u542b\u00a0<code class=\"filename\">boost\/interprocess\/mapped_region.hpp<\/code>\u00a0\u5934\u6587\u4ef6\u3002\u00a0<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u7684\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662f\u00a0<code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u793a\u6b64\u5185\u5b58\u533a\u57df\u5bf9\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u662f\u53ea\u8bfb\u6216\u662f\u53ef\u5199\u7684\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u521b\u5efa\u4e86\u4e24\u4e2a\u00a0<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u7c7b\u578b\u7684\u5bf9\u8c61\u3002 \u540d\u4e3a&#8221;Highscore&#8221;\u7684\u5171\u4eab\u5185\u5b58\uff0c\u88ab\u6620\u5c04\u5230\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e24\u6b21\u3002 \u901a\u8fc7\u00a0<code class=\"methodname\">get_address()<\/code>\u00a0\u548c\u00a0<code class=\"methodname\">get_size()<\/code>\u00a0\u8fd9\u4e24\u4e2a\u51fd\u6570\u83b7\u5f97\u5171\u4eab\u5185\u5b58\u7684\u5730\u5740\u548c\u5927\u5c0f\u5199\u5230\u6807\u51c6\u6807\u51c6\u8f93\u51fa\u6d41\u4e2d\u3002 \u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0c<code class=\"methodname\">get_size()<\/code>\u00a0\u7684\u8fd4\u56de\u503c\u90fd\u662f<code class=\"computeroutput\">1024<\/code>\uff0c\u800c\u00a0<code class=\"methodname\">get_address()<\/code>\u00a0\u7684\u8fd4\u56de\u503c\u662f\u4e0d\u540c\u7684\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u5199\u5165\u5e76\u8bfb\u53d6\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/shared_memory_object.hpp&gt; \r\n#include &lt;boost\/interprocess\/mapped_region.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, \"Highscore\", boost::interprocess::read_write); \r\n  shdmem.truncate(1024); \r\n  boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write); \r\n  int *i1 = static_cast&lt;int*&gt;(region.get_address()); \r\n  *i1 = 99; \r\n  boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only); \r\n  int *i2 = static_cast&lt;int*&gt;(region2.get_address()); \r\n  std::cout &lt;&lt; *i2 &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.2.3\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u901a\u8fc7\u53d8\u91cf\u00a0<var>region<\/var>, \u6570\u503c 99 \u88ab\u5199\u5230\u5171\u4eab\u5185\u5b58\u7684\u5f00\u59cb\u5904\u3002 \u7136\u540e\u53d8\u91cf\u00a0<var>region2<\/var>\u00a0\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u7684\u540c\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u5e76\u5c06\u6570\u503c\u5199\u5165\u5230\u6807\u51c6\u8f93\u51fa\u6d41\u4e2d\u3002 \u6b63\u5982\u524d\u9762\u4f8b\u5b50\u7684\u00a0<code class=\"methodname\">get_address()<\/code>\u00a0\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6240\u89c1\uff0c\u867d\u7136\u53d8\u91cf\u00a0<var>region<\/var>\u00a0\u548c\u00a0<var>region2<\/var>\u00a0\u8868\u793a\u7684\u662f\u8be5\u8fdb\u7a0b\u5185\u4e0d\u540c\u7684\u5185\u5b58\u533a\u57df\uff0c\u4f46\u7531\u4e8e\u4e24\u4e2a\u5185\u5b58\u533a\u57df\u5e95\u5c42\u5b9e\u9645\u8bbf\u95ee\u7684\u662f\u540c\u4e00\u5757\u5171\u4eab\u5185\u5b58\uff0c\u6240\u4ee5\u7a0b\u5e8f\u6253\u5370\u51fa<code class=\"computeroutput\">99<\/code>\u3002<\/p>\n<p>\u901a\u5e38\uff0c\u4e0d\u4f1a\u5728\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5185\u4f7f\u7528\u591a\u4e2a\u00a0<code class=\"classname\">boost::interprocess::mapped_region<\/code>\u00a0\u8bbf\u95ee\u540c\u4e00\u5757\u5171\u4eab\u5185\u5b58\u3002 \u5b9e\u9645\u4e0a\u5728\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5185\u5c06\u540c\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u6620\u5c04\u5230\u4e0d\u540c\u7684\u5185\u5b58\u533a\u57df\u4e0a\u6ca1\u6709\u591a\u5927\u7684\u610f\u4e49\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u53ea\u7528\u4e8e\u8bf4\u660e\u7684\u76ee\u7684\u3002<\/p>\n<p>\u4e3a\u4e86\u5220\u9664\u6307\u5b9a\u7684\u5171\u4eab\u5185\u5b58\uff0c<code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u5bf9\u8c61\u63d0\u4f9b\u4e86\u9759\u6001\u7684\u00a0<code class=\"methodname\">remove()<\/code>\u00a0\u51fd\u6570\uff0c\u6b64\u51fd\u6570\u5e26\u6709\u4e00\u4e2a\u8981\u88ab\u5220\u9664\u7684\u5171\u4eab\u5185\u5b58\u540d\u79f0\u7684\u53c2\u6570\u3002<\/p>\n<p>Boost.Interprocess \u7c7b\u7684RAII\u6982\u5ff5\u652f\u6301\uff0c\u660e\u663e\u6765\u81ea\u5173\u4e8e\u667a\u80fd\u6307\u9488\u7684\u7ae0\u8282\uff0c\u5e76\u4f7f\u7528\u4e86\u53e6\u5916\u7684\u4e00\u4e2a\u7c7b\u540d\u79f0\u00a0<code class=\"classname\">boost::interprocess::remove_shared_memory_on_destroy<\/code>\u3002 \u5b83\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5171\u4eab\u5185\u5b58\u7684\u540d\u79f0\u3002 \u5982\u679c\u8fd9\u4e2a\u7c7b\u7684\u5bf9\u8c61\u88ab\u9500\u6bc1\u4e86\uff0c\u90a3\u4e48\u5728\u6790\u6784\u51fd\u6570\u4e2d\u4f1a\u81ea\u52a8\u5220\u9664\u5171\u4eab\u5185\u5b58\u7684\u5bb9\u5668\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\u6784\u9020\u51fd\u6570\u5e76\u4e0d\u521b\u5efa\u6216\u6253\u5f00\u5171\u4eab\u5185\u5b58\uff0c\u6240\u4ee5\uff0c\u8fd9\u4e2a\u7c7b\u5e76\u4e0d\u662f\u5178\u578bRAII\u6982\u5ff5\u7684\u4ee3\u8868\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/shared_memory_object.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  bool removed = boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n  std::cout &lt;&lt; removed &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.2.4\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u5982\u679c\u00a0<code class=\"methodname\">remove()<\/code>\u00a0\u6ca1\u6709\u88ab\u8c03\u7528, \u90a3\u4e48\uff0c\u5373\u4f7f\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u5171\u4eab\u5185\u5b58\u8fd8\u4f1a\u4e00\u76f4\u5b58\u5728\uff0c\u800c\u4e0d\u8bba\u5171\u4eab\u5185\u5b58\u7684\u5220\u9664\u662f\u5426\u4f9d\u8d56\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u3002 \u591a\u6570Unix\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5305\u62ecLinux\uff0c\u4e00\u65e6\u7cfb\u7edf\u91cd\u65b0\u542f\u52a8\uff0c\u90fd\u4f1a\u81ea\u52a8\u5220\u9664\u5171\u4eab\u5185\u5b58\uff0c\u5728 Windows \u6216 Mac OS X\u4e0a\uff0c<code class=\"methodname\">remove()<\/code>\u00a0\u5fc5\u987b\u8c03\u7528\uff0c\u8fd9\u4e24\u79cd\u7cfb\u7edf\u5b9e\u9645\u4e0a\u5c06\u5171\u4eab\u5185\u5b58\u5b58\u50a8\u5728\u6301\u4e45\u5316\u7684\u6587\u4ef6\u4e0a\uff0c\u6b64\u6587\u4ef6\u5728\u7cfb\u7edf\u91cd\u542f\u540e\u8fd8\u662f\u5b58\u5728\u7684\u3002<\/p>\n<p>Windows \u63d0\u4f9b\u4e86\u4e00\u79cd\u7279\u522b\u7684\u5171\u4eab\u5185\u5b58\uff0c\u5b83\u53ef\u4ee5\u5728\u6700\u540e\u4e00\u4e2a\u4f7f\u7528\u5b83\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec8\u6b62\u540e\u81ea\u52a8\u5220\u9664\u3002 \u4e3a\u4e86\u4f7f\u7528\u5b83\uff0c\u63d0\u4f9b\u4e86\u00a0<code class=\"classname\">boost::interprocess::windows_shared_memory<\/code>\u00a0\u7c7b\uff0c\u5b9a\u4e49\u5728\u00a0<code class=\"filename\">boost\/interprocess\/windows_shared_memory.hpp<\/code>\u00a0\u6587\u4ef6\u4e2d\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/windows_shared_memory.hpp&gt; \r\n#include &lt;boost\/interprocess\/mapped_region.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::windows_shared_memory shdmem(boost::interprocess::open_or_create, \"Highscore\", boost::interprocess::read_write, 1024); \r\n  boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write); \r\n  int *i1 = static_cast&lt;int*&gt;(region.get_address()); \r\n  *i1 = 99; \r\n  boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only); \r\n  int *i2 = static_cast&lt;int*&gt;(region2.get_address()); \r\n  std::cout &lt;&lt; *i2 &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.2.5\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u8bf7\u6ce8\u610f\uff0c<code class=\"classname\">boost::interprocess::windows_shared_memory<\/code>\u00a0\u7c7b\u6ca1\u6709\u63d0\u4f9b\u00a0<code class=\"methodname\">truncate()<\/code>\u00a0\u51fd\u6570\uff0c\u800c\u662f\u5728\u6784\u9020\u51fd\u6570\u7684\u7b2c\u56db\u4e2a\u53c2\u6570\u4f20\u9012\u5171\u4eab\u5185\u5b58\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u5373\u4f7f\u00a0<code class=\"classname\">boost::interprocess::windows_shared_memory<\/code>\u00a0\u7c7b\u662f\u4e0d\u53ef\u79fb\u690d\u7684\uff0c\u4e14\u53ea\u80fd\u7528\u4e8eWindows\u7cfb\u7edf\uff0c\u4f46\u4f7f\u7528\u8fd9\u79cd\u7279\u522b\u7684\u5171\u4eab\u5185\u5b58\u5728\u4e0d\u540c\u5e94\u7528\u4e4b\u95f4\u4ea4\u6362\u6570\u636e\uff0c\u5b83\u8fd8\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<\/div>\n<hr \/>\n<h2 class=\"title\"><a name=\"interprocesscommunication_managed_shared_memory\"><\/a>8.3.\u00a0\u6258\u7ba1\u5171\u4eab\u5185\u5b58<\/h2>\n<div class=\"sect1\">\n<p>\u4e0a\u4e00\u8282\u4ecb\u7ecd\u4e86\u7528\u6765\u521b\u5efa\u548c\u7ba1\u7406\u5171\u4eab\u7684\u00a0<code class=\"classname\">boost::interprocess::shared_memory_object<\/code>\u00a0\u7c7b\u3002 \u5b9e\u9645\u4e0a\uff0c\u7531\u4e8e\u8fd9\u4e2a\u7c7b\u9700\u8981\u6309\u5355\u4e2a\u5b57\u8282\u7684\u65b9\u5f0f\u8bfb\u5199\u5171\u4eab\u5185\u5b58\uff0c\u6240\u4ee5\u8fd9\u4e2a\u7c7b\u51e0\u4e4e\u4e0d\u7528\u3002 \u6982\u5ff5\u4e0a\u6765\u8bb2\uff0cC++\u6539\u5584\u4e86\u7c7b\u5bf9\u8c61\u7684\u521b\u5efa\u5e76\u9690\u85cf\u4e86\u5b83\u4eec\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u54ea\u91cc\uff0c\u662f\u600e\u4eec\u5b58\u50a8\u7684\u8fd9\u4e9b\u7ec6\u8282\u3002<\/p>\n<p>Boost.Interprocess \u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a\u201c\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u201d\u7684\u6982\u5ff5\uff0c\u901a\u8fc7\u5b9a\u4e49\u5728\u00a0<code class=\"filename\">boost\/interprocess\/managed_shared_memory.hpp<\/code>\u00a0\u6587\u4ef6\u4e2d\u7684\u00a0<code class=\"classname\">boost::interprocess::managed_shared_memory<\/code>\u00a0\u7c7b\u63d0\u4f9b\u3002 \u8fd9\u4e2a\u7c7b\u7684\u76ee\u7684\u662f\uff0c\u5bf9\u4e8e\u9700\u8981\u5206\u914d\u5230\u5171\u4eab\u5185\u5b58\u4e0a\u7684\u5bf9\u8c61\uff0c\u5b83\u80fd\u591f\u4ee5\u5185\u5b58\u7533\u8bf7\u7684\u65b9\u5f0f\u521d\u59cb\u5316\uff0c\u5e76\u4f7f\u5176\u81ea\u52a8\u4e3a\u4f7f\u7528\u540c\u4e00\u4e2a\u5171\u4eab\u5185\u5b58\u7684\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u53ef\u7528\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"Highscore\", 1024); \r\n  int *i = managed_shm.construct&lt;int&gt;(\"Integer\")(99); \r\n  std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n  std::pair&lt;int*, std::size_t&gt; p = managed_shm.find&lt;int&gt;(\"Integer\"); \r\n  if (p.first) \r\n    std::cout &lt;&lt; *p.first &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.3.1\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u6253\u5f00\u540d\u4e3a &#8220;Highscore&#8221; \u5927\u5c0f\u4e3a1,024 \u5b57\u8282\u7684\u5171\u4eab\u5185\u5b58\uff0c\u5982\u679c\u5b83\u4e0d\u5b58\u5728\uff0c\u5b83\u4f1a\u88ab\u81ea\u52a8\u5730\u521b\u5efa\u3002<\/p>\n<p>\u5728\u5e38\u89c4\u7684\u5171\u4eab\u5185\u5b58\u4e2d\uff0c\u4e3a\u4e86\u8bfb\u5199\u6570\u636e\uff0c\u5355\u4e2a\u5b57\u8282\u88ab\u76f4\u63a5\u8bbf\u95ee\uff0c\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4f7f\u7528\u8bf8\u5982\u00a0<code class=\"methodname\">construct()<\/code>\u00a0\u51fd\u6570\uff0c\u6b64\u51fd\u6570\u8981\u6c42\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u4f5c\u4e3a\u6a21\u677f\u53c2\u6570\uff0c\u6b64\u4f8b\u4e2d\u58f0\u660e\u7684\u662f\u00a0<code class=\"type\">int<\/code>\u00a0\u7c7b\u578b\uff0c\u51fd\u6570\u7f3a\u7701\u8981\u6c42\u4e00\u4e2a\u540d\u79f0\u6765\u8868\u793a\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u521b\u5efa\u7684\u5bf9\u8c61\u3002 \u6b64\u4f8b\u4e2d\u4f7f\u7528\u7684\u540d\u79f0\u662f &#8220;Integer&#8221;\u3002<\/p>\n<p>\u7531\u4e8e\u00a0<code class=\"methodname\">construct()<\/code>\u00a0\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61\uff0c\u4e3a\u4e86\u521d\u59cb\u5316\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u7ed9\u6b64\u51fd\u6570\u3002 \u8bed\u6cd5\u770b\u4e0a\u53bb\u50cf\u8c03\u7528\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u3002 \u8fd9\u5c31\u786e\u4fdd\u4e86\u5bf9\u8c61\u4e0d\u4ec5\u80fd\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u521b\u5efa\uff0c\u8fd8\u80fd\u591f\u6309\u9700\u8981\u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5b83\u3002<\/p>\n<p>\u4e3a\u4e86\u8bbf\u95ee\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e0a\u7684\u4e00\u4e2a\u7279\u5b9a\u5bf9\u8c61\uff0c\u7528\u00a0<code class=\"methodname\">find()<\/code>\u00a0\u51fd\u6570\u3002 \u901a\u8fc7\u4f20\u9012\u8981\u67e5\u627e\u5bf9\u8c61\u7684\u540d\u79f0\uff0c\u8fd4\u56de\u6216\u8005\u662f\u4e00\u4e2a\u6307\u5411\u8fd9\u4e2a\u7279\u5b9a\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u6216\u8005\u662f0\u8868\u793a\u7ed9\u5b9a\u540d\u79f0\u7684\u5bf9\u8c61\u6ca1\u6709\u627e\u5230\u3002<\/p>\n<p>\u6b63\u5982\u524d\u9762\u4f8b\u5b50\u4e2d\u6240\u89c1\uff0c<code class=\"methodname\">find()<\/code>\u00a0\u5b9e\u9645\u8fd4\u56de\u7684\u662f\u00a0<code class=\"classname\">std::pair<\/code>\u00a0\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c<var>first<\/var>\u00a0\u5c5e\u6027\u63d0\u4f9b\u7684\u662f\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u90a3\u4e48\u00a0<var>second<\/var>\u00a0\u5c5e\u6027\u63d0\u4f9b\u7684\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"Highscore\", 1024); \r\n  int *i = managed_shm.construct&lt;int&gt;(\"Integer\")[10](99); \r\n  std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n  std::pair&lt;int*, std::size_t&gt; p = managed_shm.find&lt;int&gt;(\"Integer\"); \r\n  if (p.first) \r\n  { \r\n    std::cout &lt;&lt; *p.first &lt;&lt; std::endl; \r\n    std::cout &lt;&lt; p.second &lt;&lt; std::endl; \r\n  } \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.3.2\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u6b21\uff0c\u901a\u8fc7\u5728\u00a0<code class=\"methodname\">construct()<\/code>\u00a0\u51fd\u6570\u540e\u9762\u7ed9\u4ee5\u7528\u65b9\u62ec\u53f7\u62ec\u4f4f\u7684\u6570\u5b5710\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a10\u4e2a\u5143\u7d20\u7684\u00a0<code class=\"type\">int<\/code>\u00a0\u7c7b\u578b\u7684\u6570\u7ec4\u3002 \u5c06\u00a0<var>second<\/var>\u00a0\u5c5e\u6027\u5199\u5230\u6807\u51c6\u8f93\u51fa\u6d41\uff0c\u540c\u6837\u662f\u8fd9\u4e2a\u6570\u5b57<code class=\"computeroutput\">10<\/code>\u3002 \u4f7f\u7528\u8fd9\u4e2a\u5c5e\u6027\uff0c<code class=\"methodname\">find()<\/code>\u00a0\u51fd\u6570\u8fd4\u56de\u7684\u5bf9\u8c61\u80fd\u591f\u533a\u5206\u662f\u5355\u4e2a\u5bf9\u8c61\u8fd8\u662f\u6570\u7ec4\u5bf9\u8c61\u3002 \u5bf9\u4e8e\u524d\u8005\uff0c<var>second<\/var>\u00a0\u7684\u503c\u662f1\uff0c\u800c\u5bf9\u4e8e\u540e\u8005\uff0c\u5b83\u7684\u503c\u662f\u6570\u7ec4\u5143\u7d20\u7684\u4e2a\u6570\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u88ab\u521d\u59cb\u5316\u4e3a\u6570\u503c99\u3002 \u4e0d\u53ef\u80fd\u6bcf\u4e2a\u5143\u7d20\u521d\u59cb\u5316\u4e3a\u4e0d\u540c\u7684\u503c\u3002<\/p>\n<p>\u5982\u679c\u7ed9\u5b9a\u540d\u79f0\u7684\u5bf9\u8c61\u5df2\u7ecf\u5728\u6258\u7ba1\u7684\u5171\u4eab\u5185\u5b58\u4e2d\u5b58\u5728\uff0c\u90a3\u4e48\u00a0<code class=\"methodname\">construct()<\/code>\u00a0\u5c06\u4f1a\u5931\u8d25\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code class=\"methodname\">construct()<\/code>\u00a0\u8fd4\u56de\u503c\u662f0\u3002 \u5982\u679c\u5b58\u5728\u7684\u5bf9\u8c61\u5373\u4f7f\u5b58\u5728\u4e5f\u53ef\u4ee5\u88ab\u91cd\u7528\uff0c<code class=\"methodname\">find_or_construct()<\/code>\u00a0\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\uff0c\u6b64\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u5b83\u627e\u5230\u7684\u5bf9\u8c61\u7684\u6307\u9488\u3002 \u6b64\u65f6\u6ca1\u6709\u521d\u59cb\u5316\u52a8\u4f5c\u53d1\u751f\u3002<\/p>\n<p>\u5176\u4ed6\u53ef\u4ee5\u5bfc\u81f4\u00a0<code class=\"methodname\">construct()<\/code>\u00a0\u5931\u8d25\u7684\u60c5\u51b5\u5982\u4e0b\u4f8b\u6240\u793a\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  try \r\n  { \r\n    boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n    boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"Highscore\", 1024); \r\n    int *i = managed_shm.construct&lt;int&gt;(\"Integer\")[4096](99); \r\n  } \r\n  catch (boost::interprocess::bad_alloc &amp;ex) \r\n  { \r\n    std::cerr &lt;&lt; ex.what() &lt;&lt; std::endl; \r\n  } \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.3.3\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u5e94\u7528\u7a0b\u5e8f\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u00a0<code class=\"type\">int<\/code>\u00a0\u7c7b\u578b\u7684\uff0c\u5305\u542b4,096\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\u3002 \u7136\u800c\uff0c\u5171\u4eab\u5185\u5b58\u53ea\u67091,024 \u5b57\u8282\uff0c\u4e8e\u662f\u7531\u4e8e\u5171\u4eab\u5185\u5b58\u4e0d\u80fd\u63d0\u4f9b\u8bf7\u6c42\u7684\u5185\u5b58\uff0c\u800c\u629b\u51fa\u00a0<code class=\"classname\">boost::interprocess::bad_alloc<\/code>\u00a0\u7c7b\u578b\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u4e00\u65e6\u5bf9\u8c61\u5df2\u7ecf\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u521b\u5efa\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u00a0<code class=\"methodname\">destroy()<\/code>\u00a0\u51fd\u6570\u5220\u9664\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"Highscore\", 1024); \r\n  int *i = managed_shm.find_or_construct&lt;int&gt;(\"Integer\")(99); \r\n  std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n  managed_shm.destroy&lt;int&gt;(\"Integer\"); \r\n  std::pair&lt;int*, std::size_t&gt; p = managed_shm.find&lt;int&gt;(\"Integer\"); \r\n  std::cout &lt;&lt; p.first &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.3.4\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u7531\u4e8e\u5b83\u662f\u4e00\u4e2a\u53c2\u6570\u7684\uff0c\u8981\u88ab\u5220\u9664\u5bf9\u8c61\u7684\u540d\u79f0\u4f20\u9012\u7ed9\u00a0<code class=\"methodname\">destroy()<\/code>\u00a0\u51fd\u6570\u3002 \u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u68c0\u67e5\u6b64\u51fd\u6570\u7684\u00a0<code class=\"type\">bool<\/code>\u00a0\u7c7b\u578b\u7684\u8fd4\u56de\u503c\uff0c\u4ee5\u786e\u5b9a\u662f\u5426\u7ed9\u5b9a\u7684\u5bf9\u8c61\u88ab\u627e\u5230\u5e76\u6210\u529f\u5220\u9664\u3002 \u7531\u4e8e\u5bf9\u8c61\u5982\u679c\u88ab\u627e\u5230\u603b\u662f\u88ab\u5220\u9664\uff0c\u6240\u4ee5\u8fd4\u56de\u503c\u00a0<code class=\"literal\">false<\/code>\u00a0\u8868\u793a\u7ed9\u5b9a\u540d\u79f0\u7684\u5bf9\u8c61\u6ca1\u6709\u627e\u5230\u3002<\/p>\n<p>\u9664\u4e86\u00a0<code class=\"methodname\">destroy()<\/code>\u00a0\u51fd\u6570\uff0c\u8fd8\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u4e2a\u51fd\u6570\u00a0<code class=\"methodname\">destroy_ptr()<\/code>\uff0c\u5b83\u80fd\u591f\u4f20\u9012\u4e00\u4e2a\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5b83\u4e5f\u80fd\u7528\u6765\u5220\u9664\u6570\u7ec4\u3002<\/p>\n<p>\u7531\u4e8e\u6258\u7ba1\u5185\u5b58\u5f88\u5bb9\u6613\u7528\u6765\u5b58\u50a8\u5728\u4e0d\u540c\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u7684\u5bf9\u8c61\uff0c\u90a3\u4e48\u5f88\u81ea\u7136\u5c31\u4f1a\u4f7f\u7528\u5230\u6765\u81eaC++\u6807\u51c6\u6a21\u677f\u5e93\u7684\u5bb9\u5668\u4e86\u3002 \u8fd9\u4e9b\u5bb9\u5668\u9700\u8981\u7528\u00a0<code class=\"code\">new<\/code>\u00a0\u8fd9\u79cd\u65b9\u5f0f\u6765\u5206\u914d\u5404\u81ea\u9700\u8981\u7684\u5185\u5b58\u3002 \u4e3a\u4e86\u5728\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e0a\u4f7f\u7528\u8fd9\u4e9b\u5bb9\u5668\uff0c\u8fd9\u5c31\u9700\u8981\u66f4\u52a0\u4ed4\u7ec6\u5730\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u5206\u914d\u5185\u5b58\u3002<\/p>\n<p>\u53ef\u60dc\u7684\u662f\uff0c\u8bb8\u591aC++\u6807\u51c6\u6a21\u677f\u5e93\u7684\u5b9e\u73b0\u5e76\u4e0d\u592a\u7075\u6d3b\uff0c\u4e0d\u80fd\u591f\u63d0\u4f9b Boost.Interprocess \u4f7f\u7528\u00a0<code class=\"classname\">std::string<\/code>\u00a0\u6216\u00a0<code class=\"classname\">std::list<\/code>\u00a0\u7684\u5bb9\u5668\u3002 \u79fb\u690d\u5230 Microsoft Visual Studio 2008 \u7684\u6807\u51c6\u6a21\u677f\u5e93\u5b9e\u73b0\u5c31\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n<p>\u4e3a\u4e86\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6709\u540d\u7684\u6765\u81eaC++\u6807\u51c6\u7684\u5bb9\u5668\uff0cBoost.Interprocess \u5728\u547d\u540d\u7a7a\u95f4\u00a0<code class=\"code\">boost::interprocess<\/code>\u00a0\u4e0b\uff0c\u63d0\u4f9b\u4e86\u5b83\u4eec\u7684\u66f4\u7075\u6d3b\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002 \u5982\uff0c<code class=\"classname\">boost::interprocess::string<\/code>\u00a0\u7684\u884c\u4e3a\u5b9e\u9645\u4e0a\u5bf9\u5e94\u7684\u662f\u00a0<code class=\"classname\">std::string<\/code>\uff0c\u4f18\u70b9\u662f\u5b83\u7684\u5bf9\u8c61\u80fd\u591f\u5b89\u5168\u5730\u5b58\u50a8\u5728\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e0a\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;boost\/interprocess\/allocators\/allocator.hpp&gt; \r\n#include &lt;boost\/interprocess\/containers\/string.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"Highscore\", 1024); \r\n  typedef boost::interprocess::allocator&lt;char, boost::interprocess::managed_shared_memory::segment_manager&gt; CharAllocator; \r\n  typedef boost::interprocess::basic_string&lt;char, std::char_traits&lt;char&gt;, CharAllocator&gt; string; \r\n  string *s = managed_shm.find_or_construct&lt;string&gt;(\"String\")(\"Hello!\", managed_shm.get_segment_manager()); \r\n  s-&gt;insert(5, \", world\"); \r\n  std::cout &lt;&lt; *s &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.3.5\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u521b\u5efa\u5728\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e0a\u7533\u8bf7\u5185\u5b58\u7684\u5b57\u7b26\u4e32\uff0c\u5fc5\u987b\u4e3a Boost.Interprocess \u63d0\u4f9b\u7684\u53e6\u5916\u4e00\u4e2a\u5206\u914d\u5668\u5b9a\u4e49\u5bf9\u5e94\u7684\u6570\u636e\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528C++\u6807\u51c6\u63d0\u4f9b\u7684\u7f3a\u7701\u5206\u914d\u5668\u3002<\/p>\n<p>\u4e3a\u4e86\u8fd9\u4e2a\u76ee\u7684\uff0cBoost.Interprocess \u5728\u00a0<code class=\"filename\">boost\/interprocess\/allocators\/allocator.hpp<\/code>\u00a0\u6587\u4ef6\u4e2d\u63d0\u4f9b\u4e86\u00a0<code class=\"classname\">boost::interprocess::allocator<\/code>\u00a0\u7c7b\u7684\u5b9a\u4e49\u3002 \u4f7f\u7528\u8fd9\u4e2a\u7c7b\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5206\u914d\u5668\uff0c\u6b64\u5206\u914d\u5668\u7684\u5185\u90e8\u4f7f\u7528\u7684\u662f\u201c\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u6bb5\u7ba1\u7406\u5668\u201d\u3002 \u6bb5\u7ba1\u7406\u5668\u8d1f\u8d23\u7ba1\u7406\u4f4d\u4e8e\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e4b\u5185\u7684\u5185\u5b58\u3002 \u4f7f\u7528\u65b0\u5efa\u7684\u5206\u914d\u5668\uff0c\u4e0e string \u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\u88ab\u5b9a\u4e49\u4e86\u3002 \u5982\u4e0a\u9762\u6240\u793a\uff0c\u5b83\u4f7f\u7528\u00a0<code class=\"classname\">boost::interprocess::basic_string<\/code>\u00a0\u800c\u4e0d\u662f\u00a0<code class=\"classname\">std::basic_string<\/code>\u3002 \u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u65b0\u6570\u636e\u7c7b\u578b\u7b80\u5355\u5730\u547d\u540d\u4e3a\u00a0<code class=\"classname\">string<\/code>\uff0c\u5b83\u662f\u57fa\u4e8e\u00a0<code class=\"classname\">boost::interprocess::basic_string<\/code>\u5e76\u7ecf\u8fc7\u5206\u914d\u5668\u8bbf\u95ee\u6bb5\u7ba1\u7406\u5668\u3002 \u4e3a\u4e86\u8ba9\u901a\u8fc7\u00a0<code class=\"methodname\">find_or_construct()<\/code>\u00a0\u521b\u5efa\u7684\u00a0<code class=\"classname\">string<\/code>\u00a0\u7279\u5b9a\u5b9e\u4f8b\uff0c\u77e5\u9053\u54ea\u4e2a\u6bb5\u7ba1\u7406\u5668\u5e94\u8be5\u88ab\u8bbf\u95ee\uff0c\u76f8\u5e94\u7684\u6bb5\u7ba1\u7406\u5668\u7684\u6307\u9488\u4f20\u9012\u7ed9\u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u4e0e\u00a0<code class=\"classname\">boost::interprocess::string<\/code>\u00a0\u4e00\u8d77, Boost.Interprocess \u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6C++\u6807\u51c6\u4e2d\u5df2\u77e5\u7684\u5bb9\u5668\u3002 \u5982,\u00a0<code class=\"classname\">boost::interprocess::vector<\/code>\u00a0\u548c\u00a0<code class=\"classname\">boost::interprocess::map<\/code>\uff0c\u5206\u522b\u5b9a\u4e49\u5728\u00a0<code class=\"filename\">boost\/interprocess\/containers\/vector.hpp<\/code>\u00a0\u548c<code class=\"filename\">boost\/interprocess\/containers\/map.hpp<\/code>\u6587\u4ef6\u4e2d<\/p>\n<p>\u65e0\u8bba\u4f55\u65f6\u540c\u4e00\u4e2a\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u88ab\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\uff0c\u8bf8\u5982\u521b\u5efa\uff0c\u67e5\u627e\u548c\u9500\u6bc1\u5bf9\u8c61\u7684\u64cd\u4f5c\u662f\u81ea\u52a8\u540c\u6b65\u7684\u3002 \u5982\u679c\u4e24\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5c1d\u8bd5\u5728\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u4e0a\u521b\u5efa\u4e0d\u540c\u540d\u79f0\u7684\u5bf9\u8c61\uff0c\u8bbf\u95ee\u76f8\u5e94\u5730\u88ab\u4e32\u884c\u5316\u4e86\u3002 \u4e3a\u4e86\u7acb\u523b\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\u800c\u4e0d\u88ab\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u7684\u64cd\u4f5c\u6253\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528<code class=\"methodname\">atomic_func()<\/code>\u00a0\u51fd\u6570\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;boost\/bind.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nvoid construct_objects(boost::interprocess::managed_shared_memory &amp;managed_shm) \r\n{ \r\n  managed_shm.construct&lt;int&gt;(\"Integer\")(99); \r\n  managed_shm.construct&lt;float&gt;(\"Float\")(3.14); \r\n} \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::shared_memory_object::remove(\"Highscore\"); \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"Highscore\", 1024); \r\n  managed_shm.atomic_func(boost::bind(construct_objects, boost::ref(managed_shm))); \r\n  std::cout &lt;&lt; *managed_shm.find&lt;int&gt;(\"Integer\").first &lt;&lt; std::endl; \r\n  std::cout &lt;&lt; *managed_shm.find&lt;float&gt;(\"Float\").first &lt;&lt; std::endl; \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.3.6\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p><code class=\"methodname\">atomic_func()<\/code>\u00a0\u9700\u8981\u4e00\u4e2a\u65e0\u53c2\u6570\uff0c\u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4f5c\u4e3a\u5b83\u7684\u53c2\u6570\u3002 \u88ab\u4f20\u9012\u7684\u51fd\u6570\u5c06\u4ee5\u4ee5\u4e00\u79cd\u786e\u4fdd\u6392\u4ed6\u8bbf\u95ee\u6258\u7ba1\u5171\u4eab\u5185\u5b58\u7684\u65b9\u5f0f\u88ab\u8c03\u7528\uff0c\u4f46\u4ec5\u9650\u4e8e\u5bf9\u8c61\u7684\u521b\u5efa\uff0c\u67e5\u627e\u548c\u9500\u6bc1\u64cd\u4f5c\u3002 \u5982\u679c\u53e6\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6709\u4e00\u4e2a\u6307\u5411\u6258\u7ba1\u5185\u5b58\u4e2d\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5b83\u8fd8\u662f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6307\u9488\u4fee\u6539\u8be5\u5bf9\u8c61\u7684\u3002<\/p>\n<p>Boost.Interprocess \u4e5f\u53ef\u4ee5\u7528\u6765\u540c\u6b65\u5bf9\u8c61\u7684\u8bbf\u95ee\u3002 \u7531\u4e8e Boost.Interprocess \u4e0d\u77e5\u9053\u5728\u4efb\u610f\u4e00\u4e2a\u65f6\u95f4\u70b9\u8c01\u53ef\u4ee5\u8bbf\u95ee\u67d0\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u540c\u6b65\u9700\u8981\u660e\u786e\u7684\u72b6\u6001\u6807\u5fd7\uff0c\u4e0b\u4e00\u8282\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\u63d0\u4f9b\u7684\u540c\u6b65\u65b9\u5f0f\u3002<\/p>\n<\/div>\n<hr \/>\n<h2 class=\"title\"><a name=\"interprocesscommunication_synchronization\"><\/a>8.4.\u00a0\u540c\u6b65<\/h2>\n<div class=\"sect1\">\n<p>Boost.Interprocess \u5141\u8bb8\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5e76\u53d1\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u3002 \u7531\u4e8e\u5171\u4eab\u5185\u5b58\u88ab\u5b9a\u4e49\u4e3a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u201c\u5171\u4eab\u201d\uff0c\u6240\u4ee5 Boost.Interprocess \u9700\u8981\u652f\u6301\u4e00\u4e9b\u540c\u6b65\u65b9\u5f0f\u3002<\/p>\n<p>\u5f53\u8003\u8651\u5230\u540c\u6b65\u7684\u65f6\u5019\uff0cBoost.Thread \u5f53\u7136\u6d6e\u73b0\u5728\u8111\u6d77\u91cc\u3002 \u6b63\u5982\u5728\u00a0<a class=\"xref\" title=\"\u7b2c\u00a06\u00a0\u7ae0\u00a0\u591a\u7ebf\u7a0b\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/multithreading.html\">\u7b2c\u00a06\u00a0\u7ae0\u00a0<i>\u591a\u7ebf\u7a0b<\/i><\/a>\u00a0\u6240\u89c1\uff0cBoost.Thread \u786e\u5b9e\u63d0\u4f9b\u4e86\u5404\u79cd\u6982\u5ff5\uff0c\u5982\u4e92\u65a5\u5bf9\u8c61\u548c\u6761\u4ef6\u53d8\u91cf\u6765\u540c\u6b65\u7ebf\u7a0b\u3002 \u53ef\u60dc\u7684\u662f\uff0c\u8fd9\u4e9b\u7c7b\u53ea\u80fd\u7528\u6765\u540c\u6b65\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5185\u7684\u7ebf\u7a0b\uff0c\u5b83\u4eec\u4e0d\u652f\u6301\u540c\u6b65\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u7531\u4e8e\u4e8c\u8005\u9762\u4e34\u7684\u95ee\u9898\u76f8\u540c\uff0c\u6240\u4ee5\u5728\u6982\u5ff5\u4e0a\u6ca1\u6709\u4ec0\u4e48\u5dee\u522b\u3002<\/p>\n<p>\u5f53\u8bf8\u5982\u4e92\u65a5\u5bf9\u8c61\u548c\u6761\u4ef6\u53d8\u91cf\u7b49\u540c\u6b65\u5bf9\u8c61\u4f4d\u4e8e\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u540c\u4e00\u5730\u5740\u7a7a\u95f4\u5185\u65f6\uff0c\u5f53\u7136\u5b83\u4eec\u5bf9\u4e8e\u6240\u6709\u7ebf\u7a0b\u90fd\u662f\u53ef\u4ee5\u8bbf\u95ee\u7684\uff0c\u800c\u5728\u5171\u4eab\u5185\u5b58\u65b9\u9762\u7684\u95ee\u9898\u662f\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u5728\u5f7c\u6b64\u4e4b\u95f4\u6b63\u786e\u5730\u5171\u4eab\u8fd9\u4e9b\u5bf9\u8c61\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u4e92\u65a5\u5bf9\u8c61\uff0c\u5b83\u6709\u65f6\u5019\u9700\u8981\u4ece\u53e6\u5916\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u6b64\u5bf9\u8c61\u3002<\/p>\n<p>Boost.Interprocess \u63d0\u4f9b\u4e86\u4e24\u79cd\u540c\u6b65\u5bf9\u8c61\uff0c\u533f\u540d\u5bf9\u8c61\u88ab\u76f4\u63a5\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u4e0a\uff0c\u8fd9\u4f7f\u5f97\u4ed6\u4eec\u81ea\u52a8\u5bf9\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\u53ef\u7528\u3002 \u547d\u540d\u5bf9\u8c61\u7531\u64cd\u4f5c\u7cfb\u7edf\u7ba1\u7406\uff0c\u6240\u4ee5\u5b83\u4eec\u4e0d\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u4e0a\uff0c\u5b83\u4eec\u53ef\u4ee5\u88ab\u5e94\u7528\u7a0b\u5e8f\u901a\u8fc7\u540d\u79f0\u8bbf\u95ee\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u901a\u8fc7\u00a0<code class=\"classname\">boost::interprocess::named_mutex<\/code>\u00a0\u521b\u5efa\u5e76\u4f7f\u7528\u4e00\u4e2a\u547d\u540d\u4e92\u65a5\u5bf9\u8c61\uff0c\u6b64\u7c7b\u5b9a\u4e49\u5728\u00a0<code class=\"filename\">boost\/interprocess\/sync\/named_mutex.hpp<\/code>\u00a0\u6587\u4ef6\u4e2d\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/named_mutex.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"shm\", 1024); \r\n  int *i = managed_shm.find_or_construct&lt;int&gt;(\"Integer\")(); \r\n  boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, \"mtx\"); \r\n  named_mtx.lock(); \r\n  ++(*i); \r\n  std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n  named_mtx.unlock(); \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.4.1\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u9664\u4e86\u4e00\u4e2a\u53c2\u6570\u7528\u6765\u6307\u5b9a\u4e92\u65a5\u5bf9\u8c61\u662f\u88ab\u521b\u5efa\u6216\u8005\u6253\u5f00\u4e4b\u5916\uff0c<code class=\"classname\">boost::interprocess::named_mutex<\/code>\u00a0\u7684\u6784\u9020\u51fd\u6570\u8fd8\u9700\u8981\u4e00\u4e2a\u540d\u79f0\u53c2\u6570\u3002 \u6bcf\u4e2a\u77e5\u9053\u6b64\u540d\u79f0\u7684\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u8bbf\u95ee\u8fd9\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002 \u4e3a\u4e86\u83b7\u5f97\u5bf9\u4f4d\u4e8e\u5171\u4eab\u5185\u5b58\u4e2d\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u901a\u8fc7\u8c03\u7528\u00a0<code class=\"methodname\">lock()<\/code>\u00a0\u51fd\u6570\u83b7\u5f97\u4e92\u65a5\u5bf9\u8c61\u7684\u62e5\u6709\u5173\u7cfb\u3002 \u7531\u4e8e\u4e92\u65a5\u5bf9\u8c61\u5728\u4efb\u610f\u65f6\u523b\u53ea\u80fd\u88ab\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u62e5\u6709\uff0c\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7b49\u5f85\uff0c\u76f4\u5230\u4e92\u65a5\u5bf9\u8c61\u88ab\u7b2c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u00a0<code class=\"methodname\">lock()<\/code>\u00a0\u51fd\u6570\u91ca\u653e\u3002 \u4e00\u65e6\u5e94\u7528\u7a0b\u5e8f\u83b7\u5f97\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743\uff0c\u5b83\u53ef\u4ee5\u83b7\u5f97\u4e92\u65a5\u5bf9\u8c61\u4fdd\u62a4\u7684\u8d44\u6e90\u7684\u6392\u4ed6\u8bbf\u95ee\u3002 \u5728\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u8d44\u6e90\u662f<code class=\"type\">int<\/code>\u7c7b\u7684\u53d8\u91cf\u88ab\u9012\u589e\u5e76\u5199\u5230\u6807\u51c6\u8f93\u51fa\u6d41\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u88ab\u542f\u52a8\u591a\u6b21\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u4f1a\u6253\u5370\u51fa\u548c\u524d\u4e00\u4e2a\u503c\u6bd4\u8f83\u9012\u589e1\u7684\u503c\u3002 \u611f\u8c22\u4e92\u65a5\u5bf9\u8c61\uff0c\u8bbf\u95ee\u5171\u4eab\u5185\u5b58\u548c\u53d8\u91cf\u672c\u8eab\u5728\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u662f\u540c\u6b65\u7684\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7684\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u4e86\u5b9a\u4e49\u5728\u00a0<code class=\"filename\">boost\/interprocess\/sync\/interprocess_mutex.hpp<\/code>\u00a0\u6587\u4ef6\u4e2d\u7684\u00a0<code class=\"classname\">boost::interprocess::interprocess_mutex<\/code>\u00a0\u7c7b\u7684\u533f\u540d\u5bf9\u8c61\u3002 \u4e3a\u4e86\u53ef\u4ee5\u88ab\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\uff0c\u5b83\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/interprocess_mutex.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"shm\", 1024); \r\n  int *i = managed_shm.find_or_construct&lt;int&gt;(\"Integer\")(); \r\n  boost::interprocess::interprocess_mutex *mtx = managed_shm.find_or_construct&lt;boost::interprocess::interprocess_mutex&gt;(\"mtx\")(); \r\n  mtx-&gt;lock(); \r\n  ++(*i); \r\n  std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n  mtx-&gt;unlock(); \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.4.2\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u786e\u5b9e\u548c\u524d\u4e00\u4e2a\u6709\u70b9\u50cf\u3002 \u552f\u4e00\u7684\u4e0d\u540c\u662f\u8fd9\u6b21\u4e92\u65a5\u5bf9\u8c61\u901a\u8fc7\u7528\u00a0<code class=\"classname\">boost::interprocess::managed_shared_memory<\/code>\u00a0\u7c7b\u7684\u00a0<code class=\"methodname\">construct()<\/code>\u00a0\u6216\u00a0<code class=\"methodname\">find_or_construct()<\/code>\u00a0\u51fd\u6570\u88ab\u76f4\u63a5\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u3002<\/p>\n<p>\u9664\u4e86\u00a0<code class=\"methodname\">lock()<\/code>\u00a0\u51fd\u6570\uff0c<code class=\"classname\">boost::interprocess::named_mutex<\/code>\u00a0\u548c\u00a0<code class=\"classname\">boost::interprocess::interprocess_mutex<\/code>\u00a0\u8fd8\u63d0\u4f9b\u4e86\u00a0<code class=\"methodname\">try_lock()<\/code>\u00a0\u548c\u00a0<code class=\"methodname\">timed_lock()<\/code>\u00a0\u51fd\u6570\u3002 \u5b83\u4eec\u7684\u884c\u4e3a\u548cBoost.Thread\u63d0\u4f9b\u7684\u4e92\u65a5\u5bf9\u8c61\u76f8\u5bf9\u5e94\u3002<\/p>\n<p>\u5728\u9700\u8981\u9012\u5f52\u4e92\u65a5\u5bf9\u8c61\u7684\u65f6\u5019\uff0cBoost.Interprocess \u63d0\u4f9b\u00a0<code class=\"classname\">boost::interprocess::named_recursive_mutex<\/code>\u00a0\u548c\u00a0<code class=\"classname\">boost::interprocess::interprocess_mutex<\/code>\u00a0\u4e24\u4e2a\u5bf9\u8c61\u53ef\u4f9b\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u4e92\u65a5\u5bf9\u8c61\u4fdd\u8bc1\u5171\u4eab\u8d44\u6e90\u7684\u6392\u4ed6\u8bbf\u95ee\u7684\u65f6\u5019\uff0c\u6761\u4ef6\u53d8\u91cf\u63a7\u5236\u4e86\u5728\u4ec0\u4e48\u65f6\u5019\uff0c\u8c01\u5fc5\u987b\u5177\u6709\u6392\u4ed6\u8bbf\u95ee\u6743\u3002 \u4e00\u822c\u6765\u8bb2\uff0cBoost.Interprocess \u548c Boost.Thread \u63d0\u4f9b\u7684\u6761\u4ef6\u53d8\u91cf\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\u3002 \u5b83\u4eec\u6709\u975e\u5e38\u76f8\u4f3c\u7684\u63a5\u53e3\uff0c\u4f7f\u719f\u6089 Boost.Thread \u7684\u7528\u6237\u5728\u4f7f\u7528 Boost.Interprocess \u7684\u8fd9\u4e9b\u6761\u4ef6\u53d8\u91cf\u65f6\u7acb\u523b\u6709\u4e00\u79cd\u81ea\u5728\u7684\u611f\u89c9\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/named_mutex.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/named_condition.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/scoped_lock.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"shm\", 1024); \r\n  int *i = managed_shm.find_or_construct&lt;int&gt;(\"Integer\")(0); \r\n  boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, \"mtx\"); \r\n  boost::interprocess::named_condition named_cnd(boost::interprocess::open_or_create, \"cnd\"); \r\n  boost::interprocess::scoped_lock&lt;boost::interprocess::named_mutex&gt; lock(named_mtx); \r\n  while (*i &lt; 10) \r\n  { \r\n    if (*i % 2 == 0) \r\n    { \r\n      ++(*i); \r\n      named_cnd.notify_all(); \r\n      named_cnd.wait(lock); \r\n    } \r\n    else \r\n    { \r\n      std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n      ++(*i); \r\n      named_cnd.notify_all(); \r\n      named_cnd.wait(lock); \r\n    } \r\n  } \r\n  named_cnd.notify_all(); \r\n  boost::interprocess::shared_memory_object::remove(\"shm\"); \r\n  boost::interprocess::named_mutex::remove(\"mtx\"); \r\n  boost::interprocess::named_condition::remove(\"cnd\"); \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.4.3\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u4f8b\u5b50\u4e2d\u4f7f\u7528\u7684\u6761\u4ef6\u53d8\u91cf\u7684\u7c7b\u578b\u00a0<code class=\"classname\">boost::interprocess::named_condition<\/code>\uff0c\u5b9a\u4e49\u5728\u00a0<code class=\"filename\">boost\/interprocess\/sync\/named_condition.hpp<\/code>\u00a0\u6587\u4ef6\u4e2d\u3002 \u7531\u4e8e\u5b83\u662f\u547d\u540d\u53d8\u91cf\uff0c\u6240\u4ee5\u5b83\u4e0d\u9700\u8981\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u00a0<code class=\"code\">while<\/code>\u00a0\u5faa\u73af\u9012\u589e\u4e00\u4e2a\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u00a0<code class=\"type\">int<\/code>\u00a0\u7c7b\u578b\u53d8\u91cf\u800c\u53d8\u91cf\u662f\u5728\u6bcf\u4e2a\u5faa\u73af\u5185\u91cd\u590d\u9012\u589e\uff0c\u800c\u5b83\u53ea\u5728\u6bcf\u4e24\u4e2a\u5faa\u73af\u65f6\u5199\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u4e2d\uff1a\u5199\u51fa\u7684\u53ea\u80fd\u662f\u5947\u6570\u3002<\/p>\n<p>\u6bcf\u6b21\uff0c\u5728\u53d8\u91cf\u9012\u589e1\u4e4b\u540e\uff0c\u6761\u4ef6\u53d8\u91cf\u00a0<var>named_cnd<\/var>\u00a0\u7684\u00a0<code class=\"methodname\">wait ()<\/code>\u51fd\u6570\u88ab\u8c03\u7528\u3002 \u4e00\u4e2a\u79f0\u4f5c\u9501\uff0c\u5728\u6b64\u4f8b\u4e2d\u662f\u53d8\u91cf\u00a0<var>lock<\/var>\u00a0\u88ab\u4f20\u9012\u7ed9\u6b64\u51fd\u6570\u3002 \u8fd9\u4e2a\u9501\u548c Boost.Thread \u4e2d\u7684\u9501\u542b\u4e49\u76f8\u540c\uff1a\u57fa\u4e8eRAII\u6982\u5ff5\u7684\u5728\u6784\u9020\u51fd\u6570\u4e2d\u83b7\u5f97\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743,\u5e76\u5728\u6790\u6784\u51fd\u6570\u4e2d\u91ca\u653e\u5b83\u3002<\/p>\n<p>\u5728\u00a0<code class=\"code\">while<\/code>\u00a0\u4e4b\u524d\u521b\u5efa\u7684\u9501\u56e0\u800c\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u62e5\u6709\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743\u3002 \u53ef\u662f\uff0c\u5982\u679c\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u00a0<code class=\"methodname\">wait()<\/code>\u00a0\u51fd\u6570\uff0c\u5b83\u4f1a\u88ab\u81ea\u52a8\u91ca\u653e\u3002<\/p>\n<p>\u6761\u4ef6\u53d8\u91cf\u5e38\u5e38\u7528\u6765\u7b49\u5f85\u4e00\u4e2a\u4fe1\u53f7\uff0c\u6b64\u4fe1\u53f7\u4f1a\u6307\u793a\u7b49\u5f85\u73b0\u5728\u5230\u4e86\u3002 \u540c\u6b65\u662f\u901a\u8fc7\u00a0<code class=\"methodname\">wait()<\/code>\u00a0\u548c\u00a0<code class=\"methodname\">notify_all()<\/code>\u00a0\u51fd\u6570\u63a7\u5236\u7684\u3002 \u5982\u679c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u8c03\u7528\u00a0<code class=\"methodname\">wait()<\/code>\u00a0\u51fd\u6570\uff0c\u4e00\u76f4\u5230\u5bf9\u5e94\u7684\u6761\u4ef6\u53d8\u91cf\u7684\u00a0<code class=\"methodname\">notify_all()<\/code>\u00a0\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u76f8\u5e94\u7684\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743\u624d\u4f1a\u88ab\u88ab\u91ca\u653e\u3002<\/p>\n<p>\u5982\u679c\u542f\u52a8\u6b64\u7a0b\u5e8f\uff0c\u5b83\u770b\u4e0a\u53bb\u4ec0\u4e48\u4e5f\u6ca1\u505a\uff1a\u800c\u53ea\u662f\u53d8\u91cf\u5728\u00a0<code class=\"code\">while<\/code>\u00a0\u5faa\u73af\u5185\u4ece0\u9012\u589e\u52301\uff0c\u7136\u540e\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u00a0<code class=\"methodname\">wait()<\/code>\u00a0\u7b49\u5f85\u4fe1\u53f7\u3002 \u4e3a\u4e86\u63d0\u4f9b\u8fd9\u4e2a\u4fe1\u53f7\uff0c\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u518d\u542f\u52a8\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<p>\u5e94\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u5c06\u4f1a\u5728\u8fdb\u5165\u00a0<code class=\"code\">while<\/code>\u00a0\u5faa\u73af\u4e4b\u524d\uff0c\u5c1d\u8bd5\u83b7\u5f97\u540c\u4e00\u4e2a\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743\u3002 \u8fd9\u80af\u5b9a\u662f\u6210\u529f\u7684\uff0c\u7531\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u7b2c\u4e00\u4e2a\u5b9e\u4f8b\u901a\u8fc7\u8c03\u7528\u00a0<code class=\"methodname\">wait()<\/code>\u00a0\u91ca\u653e\u4e86\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743\u3002 \u56e0\u4e3a\u53d8\u91cf\u5df2\u7ecf\u9012\u589e\u4e86\u4e00\u6b21\uff0c\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u73b0\u5728\u4f1a\u6267\u884c\u00a0<code class=\"code\">if<\/code>\u00a0\u8868\u8fbe\u5f0f\u7684\u00a0<code class=\"code\">else<\/code>\u00a0\u5206\u652f\uff0c\u8fd9\u4f7f\u5f97\u5728\u9012\u589e1\u4e4b\u524d\u5c06\u5f53\u524d\u503c\u5199\u5230\u6807\u51c6\u8f93\u51fa\u6d41\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u4e5f\u8c03\u7528\u4e86\u00a0<code class=\"methodname\">wait()<\/code>\u00a0\u51fd\u6570\uff0c\u53ef\u662f\uff0c\u5728\u8c03\u7528\u4e4b\u524d,\u5b83\u8c03\u7528\u4e86\u00a0<code class=\"methodname\">notify_all()<\/code>\u00a0\u51fd\u6570\uff0c\u8fd9\u5bf9\u4e8e\u4e24\u4e2a\u5b9e\u4f8b\u6b63\u786e\u534f\u4f5c\u662f\u975e\u5e38\u91cd\u8981\u7684\u987a\u5e8f\u3002 \u7b2c\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u901a\u77e5\u5e76\u518d\u6b21\u5c1d\u8bd5\u83b7\u5f97\u4e92\u65a5\u5bf9\u8c61\u7684\u6240\u6709\u6743\uff0c\u867d\u7136\u73b0\u5728\u5b83\u8fd8\u88ab\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u6240\u62e5\u6709\u3002 \u7531\u4e8e\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b\u5728\u8c03\u7528\u00a0<code class=\"methodname\">notify_all()<\/code>\u00a0\u4e4b\u540e\u8c03\u7528\u4e86\u00a0<code class=\"methodname\">wait()<\/code>\uff0c\u8fd9\u81ea\u52a8\u91ca\u653e\u4e86\u6240\u6709\u6743\uff0c\u7b2c\u4e00\u4e2a\u5b9e\u4f8b\u6b64\u65f6\u80fd\u591f\u83b7\u5f97\u6240\u6709\u6743\u3002<\/p>\n<p>\u4e24\u4e2a\u5b9e\u4f8b\u4ea4\u66ff\u5730\u9012\u589e\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u3002 \u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5c06\u53d8\u91cf\u503c\u5199\u5230\u6807\u51c6\u8f93\u51fa\u6d41\u3002 \u53ea\u8981\u53d8\u91cf\u503c\u5230\u8fbe10\uff0c<code class=\"code\">while<\/code>\u00a0\u5faa\u73af\u7ed3\u675f\u3002 \u4e3a\u4e86\u8ba9\u5176\u4ed6\u5b9e\u4f8b\u4e0d\u5fc5\u6c38\u8fdc\u7b49\u5f85\u4fe1\u53f7\uff0c\u00a0<code class=\"methodname\">notify_all()<\/code>\u00a0\u51fd\u6570\u5728\u5faa\u73af\u4e4b\u540e\u53c8\u88ab\u8c03\u7528\u4e86\u4e00\u6b21\u3002 \u5728\u7ec8\u6b62\u4e4b\u524d\uff0c\u5171\u4eab\u5185\u5b58\uff0c\u4e92\u65a5\u5bf9\u8c61\u548c\u6761\u4ef6\u53d8\u91cf\u90fd\u88ab\u9500\u6bc1\u3002<\/p>\n<p>\u5c31\u50cf\u6709\u4e24\u79cd\u4e92\u65a5\u5bf9\u8c61\uff0c\u5373\u5fc5\u987b\u5b58\u50a8\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u533f\u540d\u7c7b\u578b\u548c\u547d\u540d\u7c7b\u578b\uff0c\u4e5f\u5b58\u5728\u4e24\u79cd\u7c7b\u578b\u7684\u6761\u4ef6\u53d8\u91cf\u3002 \u73b0\u5728\u7528\u533f\u540d\u6761\u4ef6\u53d8\u91cf\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n<pre class=\"programlisting\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/interprocess_mutex.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/interprocess_condition.hpp&gt; \r\n#include &lt;boost\/interprocess\/sync\/scoped_lock.hpp&gt; \r\n#include &lt;iostream&gt; \r\n\r\nint main() \r\n{ \r\n  try \r\n  { \r\n    boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_or_create, \"shm\", 1024); \r\n    int *i = managed_shm.find_or_construct&lt;int&gt;(\"Integer\")(0); \r\n    boost::interprocess::interprocess_mutex *mtx = managed_shm.find_or_construct&lt;boost::interprocess::interprocess_mutex&gt;(\"mtx\")(); \r\n    boost::interprocess::interprocess_condition *cnd = managed_shm.find_or_construct&lt;boost::interprocess::interprocess_condition&gt;(\"cnd\")(); \r\n    boost::interprocess::scoped_lock&lt;boost::interprocess::interprocess_mutex&gt; lock(*mtx); \r\n    while (*i &lt; 10) \r\n    { \r\n      if (*i % 2 == 0) \r\n      { \r\n        ++(*i); \r\n        cnd-&gt;notify_all(); \r\n        cnd-&gt;wait(lock); \r\n      } \r\n      else \r\n      { \r\n        std::cout &lt;&lt; *i &lt;&lt; std::endl; \r\n        ++(*i); \r\n        cnd-&gt;notify_all(); \r\n        cnd-&gt;wait(lock); \r\n      } \r\n    } \r\n    cnd-&gt;notify_all(); \r\n  } \r\n  catch (...) \r\n  { \r\n  } \r\n  boost::interprocess::shared_memory_object::remove(\"shm\"); \r\n}<\/pre>\n<ul class=\"programlisting\">\n<li><a class=\"programlisting\" href=\"http:\/\/zh.highscore.de\/cpp\/boost\/src\/8.4.4\/main.cpp\">\u4e0b\u8f7d\u6e90\u4ee3\u7801<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u4f5c\u5b8c\u5168\u548c\u524d\u4e00\u4e2a\u4f8b\u5b50\u4e00\u6837\uff0c\u4e3a\u4e86\u9012\u589e\u53d8\u91cf10\u6b21\uff0c\u56e0\u800c\u4e5f\u9700\u8981\u542f\u52a8\u4e24\u4e2a\u5b9e\u4f8b\u3002 \u4e24\u4e2a\u4f8b\u5b50\u4e4b\u95f4\u7684\u5dee\u522b\u5f88\u5c0f\u3002 \u4e0e\u662f\u5426\u4f7f\u7528\u533f\u540d\u6216\u547d\u540d\u6761\u4ef6\u53d8\u91cf\u6839\u672c\u6ca1\u6709\u4ec0\u4e48\u5173\u7cfb\u3002<\/p>\n<p>\u5904\u7406\u4e92\u65a5\u5bf9\u8c61\u548c\u6761\u4ef6\u53d8\u91cf\uff0cBoost.Interprocess \u8fd8\u63d0\u4f9b\u4e86\u53eb\u505a\u4fe1\u53f7\u91cf\u548c\u6587\u4ef6\u9501\u3002 \u4fe1\u53f7\u91cf\u7684\u884c\u4e3a\u548c\u6761\u4ef6\u53d8\u91cf\u76f8\u4f3c\uff0c\u9664\u4e86\u5b83\u4e0d\u80fd\u533a\u522b\u4e24\u79cd\u72b6\u6001\uff0c\u4f46\u5b83\u786e\u662f\u57fa\u4e8e\u8ba1\u6570\u5668\u7684\u3002 \u6587\u4ef6\u9501\u6709\u4e9b\u50cf\u4e92\u65a5\u5bf9\u8c61\uff0c\u867d\u7136\u5b83\u4eec\u4e0d\u662f\u5173\u4e8e\u5185\u5b58\u7684\u5bf9\u8c61\uff0c\u4f46\u5b83\u4eec\u786e\u662f\u6587\u4ef6\u7cfb\u7edf\u4e0a\u5173\u4e8e\u6587\u4ef6\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5c31\u50cf Boost.Thread \u80fd\u591f\u533a\u5206\u4e0d\u540c\u7684\u4e92\u65a5\u5bf9\u8c61\u548c\u9501\uff0cBoost.Interprocess \u4e5f\u63d0\u4f9b\u4e86\u51e0\u4e2a\u4e92\u65a5\u5bf9\u8c61\u548c\u9501\u3002 \u4f8b\u5982\uff0c\u4e92\u65a5\u5bf9\u8c61\u4e0d\u4ec5\u80fd\u88ab\u6392\u4ed6\u5730\u62e5\u6709\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u6392\u4ed6\u5730\u6240\u6709\u3002 \u8fd9\u5728\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u540c\u65f6\u8bfb\u800c\u6392\u4ed6\u5199\u7684\u65f6\u5019\u975e\u5e38\u6709\u7528\u3002 \u5bf9\u4e8e\u4e0d\u540c\u7684\u4e92\u65a5\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5177\u6709RAII\u6982\u5ff5\u7684\u9501\u7c7b\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\u5982\u679c\u4e0d\u4f7f\u7528\u533f\u540d\u540c\u6b65\u5bf9\u8c61\uff0c\u90a3\u4e48\u540d\u79f0\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002 \u867d\u7136\u4e92\u65a5\u5bf9\u8c61\u548c\u6761\u4ef6\u53d8\u91cf\u662f\u57fa\u4e8e\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\uff0c\u4f46\u4e5f\u4e0d\u5fc5\u603b\u662f\u8ba4\u4e3a\u64cd\u4f5c\u7cfb\u7edf\u72ec\u7acb\u7684\u63a5\u53e3\u662f\u7531 Boost.Interprocess \u533a\u522b\u5bf9\u5f85\u7684\u3002 \u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u4e92\u65a5\u5bf9\u8c61\u548c\u6761\u4ef6\u53d8\u91cf\u4f7f\u7528\u540c\u6837\u7684\u7cfb\u7edf\u51fd\u6570\u3002 \u5982\u679c\u8fd9\u4e24\u79cd\u5bf9\u8c61\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\uff0c\u90a3\u4e48\u5e94\u7528\u7a0b\u5e8f\u5728Windows\u4e0a\u5c06\u4e0d\u4f1a\u6b63\u786e\u5730\u5740\u6267\u884c\u3002<\/p>\n<\/div>\n<hr \/>\n<h2 class=\"title\"><a name=\"interprocesscommunication_exercises\"><\/a>8.5.\u00a0\u7ec3\u4e60<\/h2>\n<div class=\"sect1\">\n<p class=\"solution\">You can buy\u00a0<a href=\"http:\/\/en.highscore.de\/shop\/index.php?p=boost-solution\" target=\"_top\">solutions to all exercises<\/a>\u00a0in this book as a ZIP file.<\/p>\n<ol>\n<li class=\"listitem\">\u521b\u5efa\u4e00\u4e2a\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u901a\u8baf\u7684\u5ba2\u6237\u7aef\/\u670d\u52a1\u5668\u5e94\u7528\u7a0b\u5e8f\u3002 \u6587\u4ef6\u540d\u79f0\u5e94\u8be5\u4f5c\u4e3a\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u7ed9\u5ba2\u6237\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002 \u8fd9\u4e2a\u6587\u4ef6\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7aef\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u7684\u76ee\u5f55\u4e0b\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5e94\u7ecf\u7531\u5171\u4eab\u5185\u5b58\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u7aef\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Boost C++ \u5e93 \u5bfc\u8a00\u7b2c\u00a01\u00a0\u7ae0\u00a0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-1422","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=1422"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1422\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}