zoukankan      html  css  js  c++  java
  • install and use boost::thread

    I am describing how to use boost::thread libaray below.

    step1. download boost from http://www.boost.org/ , e.g. boost_1_52_0.tar.gz

    step2. extract the file boost_1_52_0.tar.gz to boost_1_52_0

    step3. cd to the boost file, boost_1_52_0, and exectute sh bootstrap.sh --prefix=$HOME/boost --with-libraries=thread

    step4.  run ./b2 to generate the libaries of boost thread

    step5. run ./b2 install, so .hpp and libs are in $HOME/boost.

    torstan: ls $HOME/boost
    include lib

    ---------------------

    Here is a sample code in boost_1_52_0/libs/thread/tutorial.


    #include <boost/thread/thread.hpp>
    #include <iostream>

    class factorial
    {
    public:
    factorial(int x, int& res) : x(x), res(res) { }
    void operator()() { res = calculate(x); }
    int result() const { return res; }

    private:
    int calculate(int x) { return x <= 1 ? 1 : x * calculate(x-1); }

    private:
    int x;
    int& res;
    };

    int main()
    {
    int result;
    factorial f(5, result);
    boost::thread thrd(f);
    thrd.join();
    std::cout << "5! = " << result << std::endl;
    }

    ----------------

    all:
    g++ -c factorial.cpp -Ihttp://www.cnblogs.com/include
    g++ -o factorial factorial.o http://www.cnblogs.com/lib/libboost_system.a http://www.cnblogs.com/lib/libboost_thread.a -lpthread
    clean:
    rm factorial *.o

    ------------------

    torstan: ./factorial
    5! = 120

  • 相关阅读:
    Mac终端编写c语言程序方法
    X-code最常用快捷键
    类方法和实例方法区别
    一、SQL语句中的增、删、查、改
    从零开始,学习web前端之HTML基础
    图片 自适应 容器大小
    Java第二天 数据类型
    Java 第一天
    javascript的基础知识
    JavaScript(一)
  • 原文地址:https://www.cnblogs.com/Torstan/p/3006077.html
Copyright © 2011-2022 走看看