// =====================================================================================
// 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"