C++ Boost Thread 编程总结

1.前言

标准C++线程即将到来。CUJ预言它将衍生自Boost线程库,现在就由Bill带领我们探索一下Boost线程库。 就在几年前,用多线程执行程序还是一件非比寻常的事。然而今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率;为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序;GUI应用程序将那些费时,复杂的处理以线程的形式单独运行,以此来保证用户界面能够及时响应用户的操作。这样使用多线程的例子还有很多。

但是C++标准并没有涉及到多线程,这让程序员们开始怀疑是否可能写出多线程的C++程序。尽管不可能写出符合标准的多线程程序,但是程序员们还是会使用支持多线程的操作系统提供的多线程库来写出多线程C++程序。但是这样做至少有两个问题:这些库大部分都是用C语言完成的,如果在C++程序中要使用这些库就必须十分小心;还有,每一个操作系统都有自己的一套支持多线程的类库。因此,这样写出来得代码是没有标准可循的,也不是到处都适用的(non-portable)。Boost线程库就是为了解决所有这些问题而设计的。

Boost是由C++标准委员会类库工作组成员发起,致力于为C++开发新的类库的组织。现在它已经有近2000名成员。许多库都可以在Boost源码的发布版本中找到。为了使这些类库是线程安全的(thread-safe),Boost线程库被创建了。

许多C++专家都投身于Boost线程库的开发中。所有接口的设计都是从0开始的,并不是C线程API的简单封装。许多C++特性(比如构造函数和析构函数,函数对象(function object)和模板)都被使用在其中以使接口更加灵活。现在的版本可以在POSIX,Win32和Macintosh Carbon平台下工作。

2.常见创建线程方法

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

void hello(const char* s,int b,int c)
{
        std::cout << s << std::endl;
        std::cout << b << std::endl;
        std::cout << c << std::endl;
}


class Base{
        Base()
        {
            std::cout<<"hello world!"<<std::endl;
        }
};
int main(int argc, char* argv[])
{
        boost::thread *plan = new boost::thread(boost::bind(&hello,"hello,I am thread",1000,2000));//thrd(&Base);
        plan->join();
        boost::thread *plan2 = new boost::thread(boost::bind(&hello,"hello,I am thread2",10002,20002));//thrd(&Base);
        plan2->join();
        return 0;
}

下面这句是创建线程的语句,hello是线程调用的函数,bind是传入参数

boost::thread *plan = new boost::thread(boost::bind(&hello,"hello,I am thread",1000,2000));

运行如下命令编译:

g++ -std=c++11 main.cpp -lpthread -lboost_system -lboost_thread -o main

执行:

 ./main

结果如下:

hello,I am thread
1000
2000
hello,I am thread2
10002
20002
3.在类中创建Boost::thread线程

代码:

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

void hello(const char* s,int b,int c)
{
        std::cout << s << std::endl;
        std::cout << b << std::endl;
        std::cout << c << std::endl;
}


class Base{
public:
      const char* s_;
      int a_;
      int b_;

        Base()
        {
            std::cout<<"hello world!"<<std::endl;
        }
public:
        void threaFUnction()
        {
            boost::thread *plan2 = new boost::thread(boost::bind(&Base::hellowold,this,s_,a_,b_));//thrd(&Base);
            plan2->join();
        }
public:
       void hellowold(const char* s,int a,int b){ 
        std::cout << s << std::endl;
        std::cout << a << std::endl;
        std::cout << b << std::endl;}
 
};

/*
void Base::hellowold(const char* s,int a,int b)
{
        std::cout << s << std::endl;
        std::cout << b << std::endl;
        std::cout << c << std::endl;
}
*/


int main(int argc, char* argv[])
{
       // boost::thread *plan = new boost::thread(boost::bind(&hello,"hello,I am thread",1000,2000));//thrd(&Base);
        //plan->join();
        //boost::thread *plan2 = new boost::thread(boost::bind(&hello,"hello,I am thread2",10002,20002));//thrd(&Base);
       // plan2->join();
        Base * base = new Base();
        base->a_ = 10;
        base->b_ = 12;
        base->s_ = "hello Thread Class!";
        base->threaFUnction();
        return 0;
}

主要是如下代码:

 void threaFUnction()
        {
            boost::thread *plan2 = new boost::thread(boost::bind(&Base::hellowold,this,s_,a_,b_));//在类中子成员创建线程
            plan2->join();
        }

执行方式和上述一样,执行结果如下:

hello world!
hello Thread Class!
12
10

Reference

  1. C++ Boost Thread 编程指南
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack Ju

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值