显示页面 修订记录 反向链接 页面重命名 您载入了该文档旧的修订版! 如果您保存了它,您就会用这些数据创建一份新的修订版。 <header><nav><p><a href="http://192.168.99.100:19001/doku.php?id=start" title="[H]" class="navbar-brand" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none; float: left; height: 50px; padding: 15px; font-size: 18px; line-height: 20px; margin-left: -15px;"><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/logo.png" alt="myspace" class="pull-left" id="dw__logo" height="20"/><span style="box-sizing: border-box; display: inline-block;">myspace</span></a></p><p><span class="glyphicon glyphicon-search" style="box-sizing: border-box; position: relative; top: 1px; display: inline-block; font-family: "Glyphicons Halflings"; line-height: 1; -webkit-font-smoothing: antialiased;"></span></p><ul class="nav navbar-nav dw-action-icon list-paddingleft-2" style="list-style-type: none;"><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost" class="dropdown-toggle" title="" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none; cursor: pointer; position: relative; display: block; padding: 15px; line-height: 20px;"><em class="mdi mdi-wrench" style="box-sizing: border-box;"></em> <span class="caret" style="box-sizing: border-box; display: inline-block; width: 0px; height: 0px; margin-left: 2px; vertical-align: middle; border-top: 4px dashed; border-right: 4px solid transparent; border-left: 4px solid transparent;"></span></a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost" class="dropdown-toggle" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none; cursor: pointer; position: relative; display: block; padding: 15px; line-height: 20px;"><em class="mdi mdi-palette" style="box-sizing: border-box;"></em> <span class="caret" style="box-sizing: border-box; display: inline-block; width: 0px; height: 0px; margin-left: 2px; vertical-align: middle; border-top: 4px dashed; border-right: 4px solid transparent; border-left: 4px solid transparent;"></span></a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost" class="dropdown-toggle" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none; cursor: pointer; position: relative; display: block; padding: 15px; line-height: 20px;"><em class="mdi mdi-account" style="box-sizing: border-box;"></em> <span class="caret" style="box-sizing: border-box; display: inline-block; width: 0px; height: 0px; margin-left: 2px; vertical-align: middle; border-top: 4px dashed; border-right: 4px solid transparent; border-left: 4px solid transparent;"></span></a></p></li></ul></nav></header><p><a style="box-sizing: border-box; background-color: rgb(254, 254, 254); color: rgb(51, 122, 183); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal;"></a><span style="color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; background-color: rgb(254, 254, 254);"></span></p><p><img src="/test_images/ueditor/php/upload/image/20201021/1603281213276760.png" title="1603281213276760.png" alt="1603281213276760.png" width="450" height="230"/></p><p><br/></p><nav><hr/><ol class="breadcrumb list-paddingleft-2" style="list-style-type: none;"><li><p>您的足迹</p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=start" title="start" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none;">start</a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=docker:manager" title="docker:manager" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none;">manager</a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=docker:dokuwiki" title="docker:dokuwiki" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none;">dokuwiki</a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=docker:portainer" title="docker:portainer" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none;">portainer</a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=playground:playground" title="playground:playground" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none;">playground</a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost" title="language:cpp:boost" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none;">boost</a></p></li></ol><hr/></nav><p><br/></p><p><br/></p><p class="text-right" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; text-align: right;"><span class="pageId ml-1 label label-primary" style="box-sizing: border-box; display: inline; padding: 0.2em 0.6em 0.3em; font-size: 12px; font-weight: 700; line-height: 1; color: rgb(255, 255, 255); text-align: center; vertical-align: baseline; border-radius: 0.25em; background-color: rgb(51, 122, 183); margin-left: 0.25rem !important;">language:cpp:boost</span></p><p><br/></p><p><br/></p><aside><p><img id="iindexmenu_20602336525d99d3e56aac30" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/base.png" alt=""/><a class="nodeFdUrl" href="http://192.168.99.100:19001/doku.php?id=start" title="myspace" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); text-decoration-line: none;">myspace</a></p><p><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac31" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/plus.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac31" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folder.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">docker</a></p><p><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac35" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/minus.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac35" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folderopen.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">language</a></p><p><img src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/line.png" alt=""/><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac36" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/minusbottom.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac36" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folderopen.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">cpp</a></p><p><img src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/line.png" alt=""/><img src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/empty.png" alt=""/><img src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/joinbottom.png" alt=""/><img id="iindexmenu_20602336525d99d3e56aac37" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/page.png" alt=""/><a class="navSel" href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost" title="boost" style="box-sizing: border-box; background-color: rgb(255, 255, 153); color: rgb(0, 153, 0); text-decoration-line: none;">boost</a></p><p><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac38" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/plus.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac38" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folder.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">linux</a></p><p><img src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/join.png" alt=""/><img id="iindexmenu_20602336525d99d3e56aac313" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folderh.png" alt=""/><a class="nodeUrl" href="http://192.168.99.100:19001/doku.php?id=playground:playground" title="playground" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); text-decoration-line: none;">playground</a></p><p><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac314" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/plus.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac314" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folder.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">scm</a></p><p><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac317" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/plus.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac317" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folder.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">wiki</a></p><p><a style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0);"><img id="jindexmenu_20602336525d99d3e56aac323" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/plus.png" alt=""/></a><img id="iindexmenu_20602336525d99d3e56aac323" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/folder.png" alt=""/><a class="node" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); padding: 1px 2px;">windows</a></p><p><img src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/joinbottom.png" alt=""/><img id="iindexmenu_20602336525d99d3e56aac326" src="http://192.168.99.100:19001/lib/plugins/indexmenu/images/tango.png/page.png" alt=""/><a class="nodeUrl" href="http://192.168.99.100:19001/doku.php?id=sidebar" title="sidebar" style="box-sizing: border-box; background-color: transparent; color: rgb(0, 153, 0); text-decoration-line: none;">sidebar</a></p></aside><article><nav><ul class="nav nav-stacked nav-pills text-muted list-paddingleft-2" style="list-style-type: none;"><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost&do=edit" title="编辑本页 [e]" class="menuitem edit" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; position: relative; display: block; padding: 2px 4px; border-radius: 4px;"></a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost&do=revisions" title="修订记录 [o]" class="menuitem revs" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; position: relative; display: block; padding: 2px 4px; border-radius: 4px;"></a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost&do=backlink" title="反向链接" class="menuitem backlink" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; position: relative; display: block; padding: 2px 4px; border-radius: 4px;"></a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost&do=menuitem" title="页面重命名" class="menuitem menuitem plugin_move_page " style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; display: block; position: relative; padding: 2px 4px; border-radius: 4px;"></a></p></li><li><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#dokuwiki__top" title="回到顶部 [t]" class="menuitem top" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; position: relative; display: block; padding: 2px 4px; border-radius: 4px;"></a></p></li></ul></nav><p><br/></p><nav><h6 class="panel-heading toc-title" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; margin-top: 0px; margin-bottom: 0px; font-size: 12px; padding: 5px; border-bottom: 1px solid rgb(221, 221, 221); border-top-left-radius: 3px; border-top-right-radius: 3px; background-color: rgb(245, 245, 245); border-top-color: rgb(221, 221, 221); border-right-color: rgb(221, 221, 221); border-left-color: rgb(221, 221, 221); cursor: pointer;"><em class="mdi mdi-view-list" style="box-sizing: border-box;"></em> <em class="caret" style="box-sizing: border-box; display: inline-block; width: 0px; height: 0px; margin-left: 2px; vertical-align: middle; border-top: 4px dashed; border-right: 4px solid transparent; border-left: 4px solid transparent;"></em></h6></nav><p><br/></p><h1 class="sectionedit1 page-header" style="box-sizing: border-box; font-size: 36px; margin: 20px 0px; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">build<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#build" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h1><pre class="code bash" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">bjam link=static bjam link=static runtime-link=static</pre><hr/><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h1 class="sectionedit2 page-header" style="box-sizing: border-box; font-size: 36px; margin: 20px 0px; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">asio<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#asio" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h1><pre class="code c" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#if defined(WIN32)#define _WIN32_WINNT 0x0501#else#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif#include <unistd.h>#include <linux/aio_abi.h>#include <sys/syscall.h>#include <sys/eventfd.h>#endif#include <stdio.h>#include <stdlib.h>#include <stdint.h>#include <boost/asio.hpp>#include <boost/bind.hpp> #if defined(WIN32) static void on_read_done(boost::asio::io_service& serv,const boost::system::error_code& bsec,char* p_buf){ if (bsec){ printf("**** 发生错误 *****\n"); } else{ printf("读取到的文件内容:\n%s\n",p_buf); } serv.stop();} int main(){ HANDLE h_file = CreateFileA( "c:\\test_aio.cpp", FILE_READ_DATA,FILE_SHARE_READ,NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL ); boost::asio::io_service io_serv; boost::asio::windows::stream_handle normal_file(io_serv,h_file); char* buf = (char*)malloc(100); memset(buf,0,100); boost::asio::async_read( normal_file, boost::asio::buffer(buf,90), boost::bind(&on_read_done,boost::ref(io_serv),_1,buf) ); boost::asio::io_service::work idle(io_serv); io_serv.run(); free(buf); return 0;} #else static void on_read_done(boost::asio::io_service& serv,aio_context_t& ctx,const boost::system::error_code& bsec,std::size_t bytes){ if (bytes != sizeof(uint64_t)){ printf("**** 发生错误 *****\n"); } else{ struct io_event event_io[1]; syscall(SYS_io_getevents,ctx,1,1,event_io,0); struct iocb* p_operation = (struct iocb*)(event_io[0].obj); if (event_io[0].res2 != 0){ printf("读取数据发生错误\n"); } else if (uint64_t(event_io[0].res) != uint64_t(p_operation->aio_nbytes)){ printf("读取部分成功: 请求读取/实际读取字节敿 %llu/%lld\n",p_operation->aio_nbytes,event_io[0].res); } else{ printf("读取到的文件内容:\n%s\n",(char*)(p_operation->aio_buf)); } } serv.stop();} int main(){ // 准备异步IO上下斿 unsigned int max_io_cnt = 10; aio_context_t ctx; memset(&ctx,0,sizeof(ctx)); if (syscall(SYS_io_setup,max_io_cnt,&ctx) != 0){ printf("准备异步IO上下文失败\n"); return -1; } // 准备eventfd,关联到boost::asio::posix::stream_descriptor,发起异步读取操作,等待事件发生 boost::asio::io_service io_serv; boost::asio::posix::stream_descriptor io_notify(io_serv); int notify_fd = eventfd(0,EFD_NONBLOCK | EFD_CLOEXEC); io_notify.assign(notify_fd); uint64_t done_io_cnt = 0; boost::asio::async_read( io_notify, boost::asio::buffer(&done_io_cnt,sizeof(done_io_cnt)), boost::bind(&on_read_done,boost::ref(io_serv),boost::ref(ctx),_1,_2) ); // 发起异步读取请求 int fd = open("/home/test_aio.cpp",O_RDWR); char* buf = (char*)malloc(100); memset(buf,0,100); struct iocb* p_operation = (struct iocb*)malloc(sizeof(struct iocb)); memset(p_operation,0,sizeof(struct iocb)); p_operation->aio_lio_opcode = IOCB_CMD_PREAD; p_operation->aio_fildes = fd; p_operation->aio_buf = (uint64_t)(uintptr_t)buf; p_operation->aio_nbytes = 90; p_operation->aio_flags = IOCB_FLAG_RESFD; p_operation->aio_resfd = notify_fd; // 事件循环 if (syscall(SYS_io_submit,ctx,1,&p_operation) == 1){ boost::asio::io_service::work idle(io_serv); io_serv.run(); } else{ printf("发起异步IO请求失败\n"); } close(notify_fd); close(fd); syscall(SYS_io_destroy,ctx); free(buf); return 0;} #endif</pre><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h1 class="sectionedit3 page-header" style="box-sizing: border-box; font-size: 36px; margin: 20px 0px; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">thread<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#thread" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h1><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h2 class="sectionedit4 page-header" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 40px 0px 20px; font-size: 30px; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">第一种情况:全局函数<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#%E7%AC%AC%E4%B8%80%E7%A7%8D%E6%83%85%E5%86%B5_%E5%85%A8%E5%B1%80%E5%87%BD%E6%95%B0" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h2><pre class="code cpp" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#include <boost/thread/thread.hpp> #include <iostream> void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; } int main(int argc, char* argv[]) { boost::thread thrd(&hello); thrd.join(); return 0; }</pre><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h2 class="sectionedit5 page-header" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 40px 0px 20px; font-size: 30px; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">第二种情况:类重载operator()<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#%E7%AC%AC%E4%BA%8C%E7%A7%8D%E6%83%85%E5%86%B5_%E7%B1%BB%E9%87%8D%E8%BD%BDoperator" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h2><pre class="code cpp" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <iostream> boost::mutex io_mutex; struct count { count(int id) : id(id) { } void operator()() { for (int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(io_mutex); std::cout << id << ": " << i << std::endl; } } int id; }; int main(int argc, char* argv[]) { boost::thread thrd1(count(1)); boost::thread thrd2(count(2)); thrd1.join(); thrd2.join(); return 0; }</pre><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h2 class="sectionedit6 page-header" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 40px 0px 20px; font-size: 30px; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">第三种情况:类内部static函数<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#%E7%AC%AC%E4%B8%89%E7%A7%8D%E6%83%85%E5%86%B5_%E7%B1%BB%E5%86%85%E9%83%A8static%E5%87%BD%E6%95%B0" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h2><pre class="code cpp" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#include <boost/thread/thread.hpp>#include <iostream> class HelloWorld{public: static void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; } static void start() { boost::thread thrd( hello ); thrd.join(); } }; int main(int argc, char* argv[]){ HelloWorld::start(); return 0;}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">在这里start()和hello()方法都必须是static方法。</p><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h2 class="sectionedit7 page-header" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 40px 0px 20px; font-size: 30px; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">第四种情况:使用boost::bind<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#%E7%AC%AC%E5%9B%9B%E7%A7%8D%E6%83%85%E5%86%B5_%E4%BD%BF%E7%94%A8boostbind" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h2><pre class="code cpp" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#include <boost/thread/thread.hpp>#include <boost/bind.hpp>#include <iostream> class HelloWorld{public: void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; } void start() { boost::function0< void> f = boost::bind(&HelloWorld::hello,this); //或boost::function<void()> f = boost::bind(&HelloWorld::hello,this); boost::thread thrd( f ); thrd.join(); } }; int main(int argc, char* argv[]){ HelloWorld hello; hello.start(); return 0;}</pre><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h2 class="sectionedit8 page-header" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 40px 0px 20px; font-size: 30px; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">第五种情况:Singleton模式<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#%E7%AC%AC%E4%BA%94%E7%A7%8D%E6%83%85%E5%86%B5_singleton%E6%A8%A1%E5%BC%8F" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h2><pre class="code cpp" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#include <boost/thread/thread.hpp>#include <boost/bind.hpp>#include <iostream> class HelloWorld{public: void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; } static void start() { boost::thread thrd( boost::bind (&HelloWorld::hello,&HelloWorld::getInstance() ) ) ; thrd.join(); } static HelloWorld& getInstance() { if ( !instance ) instance = new HelloWorld; return *instance; }private: HelloWorld(){} static HelloWorld* instance; }; HelloWorld* HelloWorld::instance = 0; int main(int argc, char* argv[]){ HelloWorld::start(); return 0;}</pre><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><h2 class="sectionedit9 page-header" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin: 40px 0px 20px; font-size: 30px; padding-bottom: 9px; border-bottom: 1px solid rgb(238, 238, 238);">第六种情况:类外用类内函数<a class="anchorjs-link " href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#%E7%AC%AC%E5%85%AD%E7%A7%8D%E6%83%85%E5%86%B5_%E7%B1%BB%E5%A4%96%E7%94%A8%E7%B1%BB%E5%86%85%E5%87%BD%E6%95%B0" style="box-sizing: border-box; background-color: transparent; color: rgb(51, 122, 183); text-decoration-line: none; opacity: 0; -webkit-font-smoothing: antialiased; font-family: anchorjs-icons; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 1; padding-left: 0.375em;"></a></h2><pre class="code cpp" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; white-space: pre-wrap;">#include <boost/thread/thread.hpp>#include <boost/bind.hpp>#include <string>#include <iostream> class HelloWorld{public: void hello(const std::string& str) { std::cout <<str<< std::endl; }}; int main(int argc, char* argv[]){ HelloWorld obj; boost::thread thrd( boost::bind(&HelloWorld::hello,&obj,"Hello world, I''m a thread!" ) ) ; thrd.join(); return 0;}</pre><p><em class="mdi mdi-pencil" style="box-sizing: border-box;"></em> 编辑</p><p><span class="docInfo" style="box-sizing: border-box;">language/cpp/boost.txt</span></p></article><p><br/></p><p><br/></p><p><br/></p><footer><p><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/logo.png" alt="myspace" class="media-object"/></p><h4 class="media-heading" style="box-sizing: border-box; font-family: inherit; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 0px; margin-bottom: 5px; font-size: 18px;">myspace</h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><br/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"> </p><ul class="list-inline pull-right hidden-print list-paddingleft-2" style="list-style-type: none;"><li><p><a href="https://www.dokuwiki.org/template:bootstrap3" title="Bootstrap template for DokuWiki" target="" class="navbar-link" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none;"><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/bootstrap.png" width="20" alt="Bootstrap template for DokuWiki"/></a></p></li><li><p> </p></li><li><p><a href="https://www.php.net/" title="Powered by PHP" target="" class="navbar-link" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none;"><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/php.png" width="20" alt="Powered by PHP"/></a></p></li><li><p> </p></li><li><p><a href="http://validator.w3.org/check/referer" title="Valid HTML5" target="" class="navbar-link" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none;"><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/html5.png" width="20" alt="Valid HTML5"/></a></p></li><li><p> </p></li><li><p><a href="http://jigsaw.w3.org/css-validator/check/referer?profile=css3" title="Valid CSS" target="" class="navbar-link" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none;"><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/css3.png" width="20" alt="Valid CSS"/></a></p></li><li><p> </p></li><li><p><a href="https://www.dokuwiki.org/" title="Driven by DokuWiki" target="" class="navbar-link" style="box-sizing: border-box; background-color: transparent; color: rgb(119, 119, 119); text-decoration-line: none;"><img src="http://192.168.99.100:19001/lib/tpl/bootstrap3/images/logo.png" width="20" alt="Driven by DokuWiki"/></a></p></li></ul></footer><p><a href="http://192.168.99.100:19001/doku.php?id=language:cpp:boost#dokuwiki__top" class="back-to-top hidden-print btn btn-default btn-sm" title="跳至内容" style="box-sizing: border-box; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); text-decoration-line: none; display: block; margin-bottom: 0px; text-align: center; vertical-align: middle; touch-action: manipulation; cursor: pointer; background-image: none; border: 1px solid rgb(204, 204, 204); padding: 5px 10px; font-size: 12px; line-height: 1.5; border-radius: 3px; user-select: none; position: fixed; bottom: 10px; right: 10px; opacity: 0.8; z-index: 1024; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;"><em class="mdi mdi-chevron-up" style="box-sizing: border-box;"></em></a></p><p><br/></p><p><span class="visible-lg-block" style="box-sizing: border-box; display: block !important;"></span></p><p><img src="http://192.168.99.100:19001/lib/exe/indexer.php?id=language%3Acpp%3Aboost&1570367183" width="2" height="1" alt=""/></p><p><br/></p> 保存 预览 取消 编辑摘要 当您选择开始编辑本页,即寓示你同意将你贡献的内容按下列许可协议发布: CC Attribution-Share Alike 4.0 International