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
  • 相关阅读:
    Delphi播放铃声
    小技巧
    Delphi线程中使用waitfor返回值
    window安装、启动consul
    kali2020-bash: openvas-setup:未找到命令 ,解决办法
    zookeeper 客户端
    redis 集群
    activeMQ
    Shiro
    Eclipse Java注释模板设置详解
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4494001.html
Copyright © 2011-2022 走看看