[C++0x] Boost Thread in Vector and use lamda as thread function

// =====================================================================================
//       Filename:  main.cpp
// 
//    Description:  thread learn from http://www.corensic.com/Learn/Resources/ConcurrencyTutorialPartOne.aspx
// 
//        Version:  1.0
//        Created:  2011年08月31日 14时52分22秒
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Jin Rongbo (), jinrongbo@gmail.com
//        Company:  JDSU
// 
// =====================================================================================

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

typedef boost::shared_ptr<boost::thread> ThreadPtr;


int main()
{
    std::vector<ThreadPtr>workers;

    for (int i = 0; i < 10; ++i) {
      workers.push_back(ThreadPtr(new boost::thread([]()
                                      {
                                        std::cout << "Hello from new thread" << std::endl;
                                      })) );
    }

    std::cout << "Hello from main" << std::endl;

    //std::for_each(workers.begin(), workers.end(), myFunctor());
    std::for_each(workers.begin(), workers.end(), [](const ThreadPtr& th)
    {
      th->join();
    });


    return 0;
}
[Compile]
g++ (GCC) 4.7.0 20111119 (experimental)
CFLAGS += -O2 -DBOOST_THREAD_POSIX
CFLAGS += -m32 -std=c++0x
LDFLAGS +=-m32 -lstdc++ -pthread -L/usr/local/lib/boost/lib -lboost_thread

[Next Step]

Adopt "Boost Thread Group"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值