原文链接:http://www.cppblog.com/toMyself/archive/2010/09/22/127347.html

背景

•  今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率;为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序;

GUI应用程序将那些费时,复杂的处理以线程的形式单独运行,以此来保证用户界面能够及时响应用户的操作。这样使用多线程的例子还有很多。

•  跨平台

最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库。在创建线程时遇到了几种线程创建方式现总结如下:  

  首先看看boost::thread的构造函数吧,boost::thread有两个构造函数: 

(1)thread():构造一个表示当前执行线程的线程对象; 

(2)explicit thread(const boost::function0<void>& threadfunc): 

     boost::function0<void>可以简单看为:一个无返回(返回void),无参数的函数。这里的函数也可以是类重载operator()构成的函数;该构造函数传入的是函数对象而并非是函数指针,这样一个具有一般函数特性的类也能作为参数传入,在下面有例子。 

第一种方式:最简单方法

#include <boost/thread/thread.hpp>
#include <iostream> void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
} int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
thrd.join();
return 0;
}

第二种方式:复杂类型对象作为参数来创建线程:

#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream> boost::mutex io_mutex;
struct count
{
count(int id) : id(id) { }
void operator()()
{
for (int i = 0; i < 10; ++i)
{
boost::mutex::scoped_lock
lock(io_mutex);
std::cout << id << ": "
<< i << std::endl;
}
}
int id;
}; int main(int argc, char* argv[])
{
boost::thread thrd1(count(1));
boost::thread thrd2(count(2));
thrd1.join();
thrd2.join();
return 0;
}

第三种方式:在类内部创建线程; 

(1)类内部静态方法启动线程 

#include <boost/thread/thread.hpp>
#include <iostream>
class HelloWorld
{
public:
static void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
static void start()
{ boost::thread thrd( hello );
thrd.join();
} };
int main(int argc, char* argv[])
{
HelloWorld::start();
return 0;
}

在这里start()和hello()方法都必须是static方法。

(2)如果要求start()和hello()方法不能是静态方法则采用下面的方法创建线程:

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
class HelloWorld
{
public:
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
void start()
{
boost::function0< void> f = boost::bind(&HelloWorld::hello,this);
boost::thread thrd( f );
thrd.join();
} };
int main(int argc, char* argv[])
{
HelloWorld hello;
hello.start();
return 0;
}

(3)在Singleton模式内部创建线程:

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
class HelloWorld
{
public:
void hello()
{
std::cout <<
"Hello world, I''m a thread!"
<< std::endl;
}
static void start()
{
boost::thread thrd( boost::bind
(&HelloWorld::hello,&HelloWorld::getInstance() ) ) ;
thrd.join();
}
static HelloWorld& getInstance()
{
if ( !instance )
instance = new HelloWorld;
return *instance;
}
private:
HelloWorld(){}
static HelloWorld* instance; };
HelloWorld* HelloWorld::instance = 0;
int main(int argc, char* argv[])
{
HelloWorld::start(); return 0;
}

第四种方法:用类内部函数在类外部创建线程; 

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <string>
#include <iostream>
class HelloWorld
{
public:
void hello(const std::string& str)
{
std::cout <<str<< std::endl;
}
}; int main(int argc, char* argv[])
{
HelloWorld obj;
boost::thread thrd( boost::bind(&HelloWorld::hello,&obj,"Hello
world, I''m a thread!" ) ) ;
thrd.join();
return 0;
}

如果线程需要绑定的函数有参数则需要使用boost::bind。比如想使用 boost::thread创建一个线程来执行函数:void f(int i),如果这样写:boost::thread thrd(f)是不对的,因为thread构造函数声明接受的是一个没有参数且返回类型为void的型别,而且不提供参数i的值f也无法运行,这时就可以写:boost::thread thrd(boost::bind(f,1))。涉及到有参函数的绑定问题基本上都是boost::thread、boost::function、boost::bind结合起来使用

boost多线程使用简例的更多相关文章

  1. (七)boost库之单例类

    (七)boost库之单例类 一.boost.serialzation的单件实现 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一 ...

  2. linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...

  3. JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制

    JAVA之旅(十四)--静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制 JAVA之旅,一路有你,加油! 一.静态同步函数的锁是clas ...

  4. Boost多线程编程

    Boost多线程编程   背景 •       今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时, ...

  5. 【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式

    boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #includ ...

  6. python--selenium多线程执行用例实例/执行多个用例

    python--selenium多线程执行用例实例/执行多个用例 我们在做selenium测试的时候呢,经常会碰到一些需要执行多个用例的情况,也就是多线 程执行py程序,我们前面讲过单个的py用例怎么 ...

  7. 5.3linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在Linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到客户端的连接后,发送数据给客户端:客户端在接受到数据后 ...

  8. Boost多线程-替换MFC线程

           Mfc的多线程看起来简单,可以把线程直接压入向量,由系统类似进行调配,其实在内存的处理问题上留下了漏洞.在新线程里面载入大量流,会导致内存泄露. 方便之处:直接使用结构体传入函数参数,供 ...

  9. Python-Unittest多线程执行用例

    前言 假设执行一条脚本(.py)用例一分钟,那么100个脚本需要100分钟,当你的用例达到一千条时需要1000分钟,也就是16个多小时... 那么如何并行运行多个.py的脚本,节省时间呢?这就用到多线 ...

随机推荐

  1. How to use pthread_create && mutex?

    1 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread. ...

  2. apk下载的网址生成一个二维码

    apk提供二维码扫描下载,其实就是把apk所在的地址生成二维码. 下面提供一个在线二维码生成网址,http://cli.im/text/1833744?iID7V

  3. 51nod1126 求递推序列的第N项【递推】

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  4. Linux目录与相关配置文件讲解

    linux目录介绍及配置文件详细介绍 重要目录简介 目录名 作用 boot 与电脑启动相关,推荐单独分区. etc 存放配置文件 mnt 一般用来设置挂载点 src.sys.proc 与系统内核相关, ...

  5. Codeforces 805A/B/C

    A. Fake NP 传送门:http://codeforces.com/contest/805/problem/A 本题是一个数学问题. 给定两个正整数l,r(l≤r),对于区间[l..r]上的任一 ...

  6. StringUtils.isNotBlank 和StringUtils.isNotEmpty 的区别

    StringUtils.isNotBlank判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成下面是示例:StringUtils.isNotBlank(null) = fals ...

  7. poj 2186 强连通+缩点

    题意:有一群牛,求被所有牛都认可的牛的个数 每个连通分量建一个缩点,出度为零的缩点包含的点的个数即为要求值 如果有多个出度为零的,直接输出零,否则输出那唯一一个出度为零的缩点包含的点的个数 #incl ...

  8. 心急的C小加 贪心算法

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的 ...

  9. pomelo研究笔记-RPC服务端

    POMELO 採用多进程的架构能够非常好的实现游戏server(进程)的扩展性,达到支撑较多在线用户.减少server压力等要求. 进程间通信採用RPC的形式来完毕,pomelo的RPC实现的相当静止 ...

  10. Webx框架:Spring Schema 和 Spring Ext

    webx诞生的原因是当时市面上没有好用的web框架.如今的Web框架有非常多.然后它们背后的思想都是相似的,并且越来越趋同. Spring Schema 在传统的spring中,配置bean时须要手动 ...