boost_1_47_0的安装以及fadora15下Eclipse中的配置

来源:互联网 发布:java 登陆session用法 编辑:程序博客网 时间:2024/05/16 05:40


首先下载boost_1_47_0.tar.gz

解压:tar-zxvf boost_1_47_0.tar.gz

进入目录:cdboost_1_47_0

运行脚本bootstrap.sh(如果默认该脚本没有可执行属性,请先改写其属性sudochmod a+x bootstrap.sh

执行脚本./bootstrap.sh

按照执行后的提示,执行./b2(build boost的各个库)

bjam  install(安装库到/usr/local/lib中)



eclipse中配置编译和连接选项

Thefollowing directory should be added to compiler include paths:

/home/mweng/lib_3rd/boost_1_47_0

Thefollowing directory should be added to linker library paths:

/home/mweng/lib_3rd/boost_1_47_0/stage/lib

project->properties->gccc++ Linker->Libraries中填写使用的库(pthread),和引用库的路劲

运用thread库,编写多线程程序的时候,要首先引用库文件.a类型的库文件

project->properties->gccc++ Linker->Miscellaneous->Other objects中添加.a类型的库文件


/* * basic_atom.hpp * 线程安全的自动增量 *  Created on: 2011-08-08 *      Author: mweng */#include<boost/thread.hpp>using namespace boost;template<class T>class basic_atom:noncopyable{private:T n;typedef mutex mutex_t;mutex_t mu;public:basic_atom(T x=T()):n(x){}T operator++(){mutex_t::scoped_lock lock(mu);return ++n;}operator T(){return n;}};

/* * main.cpp * *  Created on: 2011-08-08 *      Author: mweng */#define BOOST_DATE_TIME_SOURCE#define BOOST_THREAD_NO_LIB#include<boost/thread.hpp>#include<boost/timer.hpp>#include<iostream>#include<cstring>#include"../head/basic_atom.hpp"using namespace std;using namespace boost;typedef basic_atom<int> atom_int;mutex io_mu;void printing(atom_int& x,const string &str){for(int i=0; i<5; i++){mutex::scoped_lock lock(io_mu);//锁定IO流,IO流是共享资源cout<<str <<":"<<++x<<endl;}}int main(){atom_int x;//原子操作的计数器thread(printing,ref(x),"hello");thread(printing,ref(x),"boost");this_thread::sleep(posix_time::seconds(2));return 1;}

结果:

hello:1

hello:2

hello:3

hello:4

hello:5

boost:6

boost:7

boost:8

boost:9

boost:10



原创粉丝点击