zoukankan      html  css  js  c++  java
  • 使用boost中的线程池

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

    using namespace boost;
    using namespace std;

    void runChild(const int n)
    {
        cout << "我是第" << n << "个子线程" << endl;
        sleep(1);
        cout << "进程" << n <<  "退出" << endl;
    }

    int main(int argc, char** argv)
    {
        int num;
        thread_group threads;

        if (argc < 2)
        {
            cout << "请提供一个要生成线程数的参数" << endl;
            exit(-1);
        }

        num atoi(argv[1]);

        cout << "我是主程序,我准备产生" << num << "个子线程" << endl;
        for(int i 0; i < num; i++)
        {
            threads.create_thread(bind(&runChild, i));
        }
        cout << "我是主程序,我在等子线程运行结束" << endl;
        threads.join_all();
        return 0;
    }

    编译&测试(我在我的ubuntu下测试的)

    > g++ threadgroup.cc -lboost_thread
    > ./a.out 3

    如果在freebsd4下编译的话,如果使用pthread作为线程实现的话,需要明确指出pthread使用线程库,而且默认的template深度好像不能满足boost的需求..需要在编译时加上:

    -ftemplate-depth-20 -boost
  • 相关阅读:
    eclipse 中配置查看jdk基础源代码
    网页授权获取用户基本信息
    语系/地区码
    windows 下 PHP DOITPHP框架 memcache缓存开启。
    xheditor富文本框 存值与展示问题
    逻辑处理
    ajax图片单个上传(ajaxfileupload.js)
    18 南京 D
    poj 2069
    poj 2420
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4494001.html
Copyright © 2011-2022 走看看