// boost_thread.cpp : 定义控制台应用程序的入口点。
// thread是一个跨平台的线程封装库
// 参考资料:http://blog.youkuaiyun.com/zhuxiaoyang2000/article/details/6588031
// http://blog.youkuaiyun.com/gitar520/article/details/7694984
// http://blog.youkuaiyun.com/zp373860147/article/details/8186724
// http://www.cnblogs.com/younes/archive/2010/06/06/1752745.html
// http://www.cppblog.com/toMyself/archive/2010/09/22/127347.html
#include "stdafx.h"
#include <boost/function/function0.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
#include <boost/bind.hpp>
boost::mutex io_mutex; // 互斥器:独占,用于多线程时线程同步
struct count
{
count(int id) : id(id) {}
void operator()()
{
// 没有此行,多线程执行时将是混乱的,不一定是某一线程执行完毕后再执行下一线程
// 确保执行完此函数所在线程
boost::mutex::scoped_lock lock(io_mutex);
std::cout<<"this is thread "<<id<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<id<<": "<<i<<std::endl;
}
}
int id;
};
void fun()
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout<<"this is thread 3"<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<"3: "<<i<<std::endl;
}
}
void func(const char* s, int val)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout<<s<<val<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<"4: "<<i<<std::endl;
}
}
class HelloWorld
{
public:
static void hello()
{
std::cout<<"this is thread 5"<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<"5: "<<i<<std::endl;
}
}
static void start()
{
boost::thread thrd(hello);
thrd.join();
}
static void hi(const std::string& s, int val)
{
std::cout<<s<<val<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<val<<": "<<i<<std::endl;
}
}
};
class HelloWorld2
{
public:
void hello()
{
std::cout<<"this is thread 6"<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<"6: "<<i<<std::endl;
}
}
void start()
{
boost::function0<void> f = boost::bind(&HelloWorld2::hello, this); // 类的普通成员函数带隐式this指针,相当于多了一个参数
boost::thread thrd(f);
thrd.join();
}
void hi(const std::string& s, int val)
{
std::cout<<s<<val<<std::endl;
for(int i = 0; i < 10; ++i)
{
std::cout<<val<<": "<<i<<std::endl;
}
}
};
// Boost::Thread有三个构造函数:
// 一个是thread(),构造一个表示当前执行线程的线程对象;
// 一个是explicit thread(const boost::function0<void>& threadfunc),
// 这里的boost::function0<void>可以简单看为一个无返回无参数的函数
// 一个是thread(F f,A1 a1,A2 a2,...); f是含多个参数的函数
int _tmain(int argc, _TCHAR* argv[])
{
boost::thread thrd1(count(1)); // 使用函数对象构造线程对象
boost::thread thrd2(count(2));
boost::thread thrd3(fun); // 使用无参数的函数
boost::thread thrd4(func, "this is thread ", 4); // 使用带可变长度的参数的函数,(函数名,参数1,参数2...)
// 调用顺序不一定为:1234
thrd1.join(); // 执行线程
thrd2.join(); // 如果对象标识一个线程执行体, 调用者等待线程对象执行结束
thrd3.join();
thrd4.join();
HelloWorld::start(); // 使用类的static函数
HelloWorld2 h;
h.start(); // 使用类的非static函数
boost::function0<void> f = boost::bind(&HelloWorld2::hi, &h, "this is thread ", 7);
boost::thread thrd5(f); // 在类外使用普通成员函数
thrd5.join();
boost::thread thrd6(&HelloWorld::hi, "this is thread ", 8); // 带参static
thrd6.join();
// boost::thread thrd7(&HelloWorld2::hi, "this is thread ", 9); // 模板实例化错误
// thrd7.join();
return 0;
}