Boost多线程配置
来源:互联网 发布:ubuntu教程 pdf 编辑:程序博客网 时间:2024/06/06 02:07
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void
workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout <<
"Worker: running"
<< std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout <<
"Worker: finished"
<< std::endl;
}
int
main(
int
argc,
char
* argv[])
{
std::cout <<
"main: startup"
<< std::endl;
boost::
thread
workerThread(workerFunc);
std::cout <<
"main: waiting for thread"
<< std::endl;
workerThread.join();
std::cout <<
"main: done"
<< std::endl;
return
0;
}
例子代码创建了一个boost线程对象,传递给他工作函数的参数,在完成时退出该线程。
这个简单的例子,我用来验证Boost线程库设置是否成功。以下步骤是运行这个简单例子的必备步骤。
1. 下载并安装Boost
如果你还没有安装好Boost,可以从这里下载并安装:http://www.boost.org/users/download/ 或者 http://www.boostpro.com/download ,安装好后在你的Visual Studio项目中,选择配置属性->附加:包含目录。然后输入Boost的安装目录。
完成以上步骤后,首次编译上面的代码可能会遇到如下错误:
LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-gd-1_46_1.lib'
意思是找不到库文件,在Boost中,必须建立自己的库。
2. 获得bjam
下一步是建立bjam.exe, 我发现最简单的方法是使用BoostPro提供的安装程序。在我的例子中,使用的是boostpro 1.46.1的安装程序。下载并运行此程序。对于我来说,我最喜欢的还是VS 2003.NET 和VS 2010两个版本。
在安装过程中,选择需要安装什么库,没必要全选,选择你需要的即可。
3. 创建 bjam.exe可执行文件
bjam安装后,打开命令行然后到src目录下,例如:
C:\Program Files\boost_1_46_1\tools\build\v2\engine\src
运行build.bat。注意脚本将在bin.ntx86子目录下创建bjam.exe可执行文件。
C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86
4. 更新PATH环境变量
现在添加bjam.exe到PATH环境变量中。在Windows 7,可以右键点击“计算机”(或我的电脑),选择属性,然后在高级系统设置中修改。
点击环境变量PATH,选择编辑按钮。
将目录添加进去,例如:C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86
5. 运行bjam可执行文件
在命令行,到C:\Program Files\boost_1_46_1目录下,输入bjam,程序创建过程需要等待5-15分钟。
6. 设置Visual Studio项目属性
在你的Visual Studio 项目中,选择配置属性->连接器->输入->附加依赖项,然后输入 libboost_thread-vc100-mt-gd-1_46_1.lib
在你的Visual Studio项目中,选择项目配置属性->连接器->常规->附加库目录,
告诉它stage/lib目录。
然后呢,我一开始所展示的Boost线程代码就可以编译运行了,输出如下:
main: startup
main: waiting
for
thread
Worker: running
Worker: finished
main: done
转自http://blog.okbase.net/haobao/archive/3.html
- Boost多线程配置
- boost 多线程
- boost多线程
- Boost多线程
- boost 多线程
- boost多线程
- BOOST多线程
- boost 多线程
- 使用boost开发多线程服务程序(安装和配置)
- Boost - 多线程-boost recursive_mutex用法
- Boost - 多线程-boost recursive_mutex用法
- Boost配置
- boost配置
- boost配置
- boost多线程编程
- boost 多线程使用
- Boost 多线程编程
- boost多线程简单用法
- x264中x264_reference_build_list函数解析
- 《coredump问题原理探究》windows版7.1节vector
- zju pat
- Qt QVariant类
- Crafting Winning Solutions
- Boost多线程配置
- 黑马程序员之C#学习笔记:StringBuilder字符串总结
- wamp的1 of 2 services are running问题解决
- 索引与查询速度之间的影响
- 红黑树原理,转
- Linux多线性编程小结
- 博士生女友自白:在读博士生是弱势群体
- g711 RTP传输
- 程序员最该看的30本书---------------------------国外编辑推荐