当前位置: 首页 > news >正文

boost_tutorial

Microsoft Visual Studio 2010 编译通过

/* 
 * boost::thread tutorial 
 * 
 * 
 *
 */  

#include <stdlib.h>
#include "boost/asio.hpp"
#include <iostream>  
#include <string>  

using namespace std;  

#include <boost/thread.hpp>  

//#1  
void run()  
{  
    cout << "Method #1" <<endl;  
}  

//#2  
boost::mutex mutex;  
class Count {  
    int _id;  
public:  
    Count(int id):_id(id) {}  
    void operator ()() {  
        for(int i = 0; i < 10; i++) {  
            boost::mutex::scoped_lock lock(mutex);  
            cout << "Count id:" << _id << " " << i <<endl;  
        }  
    }  
};  

//#3  
class InnerThread {  
public:  
    static void run() {  
        cout << "class inner thread: run and start must be static." <<endl;  
    }  

    static void start() {  
        boost::thread thread(&run);  
        thread.join();  
    }  
};  

class InnerThread2 {  
public:  
    void run() {  
        cout << "class inner thread: use boost::bind" <<endl;  
    }  

    void start() {  
        boost::function0<void> f = boost::bind(&InnerThread2::run, this);  
        boost::thread thread(f);  
        thread.join();  
    }  
};  

//#4  
class SingletonThread {  
public:  
    void run() {  
        cout << "Singleton thread" <<endl;  
    }  

    void start() {  
        boost::function0<void> f = boost::bind(&SingletonThread::run, SingletonThread::getInstance());  
        boost::thread thread(f);  
        thread.join();  
    }  

    static SingletonThread* getInstance() {  
        if(!_instance) {  
            _instance = new SingletonThread;  
        }  
        return _instance;  
    }  
private:  
    SingletonThread() {}  
    static SingletonThread* _instance;  
};  
SingletonThread* SingletonThread::_instance = 0;  

//#5  
class Class {  
public:  
    void run(const std::string& str) {  
        cout <<str <<endl;  
    }  

    int add(int x, int y) {  
        cout << "x+y=" <<x+y<<endl;  
        return x+y;  
    }  
};  

//#6  
int add(int x, int y) {  
    return x+y;  
}  

int main()  
{  
    cout << "boost::thread: " << endl;  

    //#1: Simple method  
    boost::thread th1(&run);  
    cout << "main thread " <<endl;  
    th1.join();  

    //#2: Through class operator() method  
    boost::thread th21(Count(1));  
    boost::thread th22(Count(2));  
    th21.join();  
    th22.join();  

    //#3: Class inner thread : static method  
    InnerThread ith;  
    ith.start();  
    InnerThread::start();  

    InnerThread2 ith2;  
    ith2.start();  

    //#4: Singleton thread  
    SingletonThread *st = SingletonThread::getInstance();  
    st->start();  

    //#5: Outer class thread  
    Class cls;  
    boost::thread outer_thread(boost::bind(&Class::run, &cls, "Outer thread"));  
    outer_thread.join();  

    //#6: Complex method: with return value  
    boost::function2<int, int, int> f = &add;  
    boost::packaged_task<int> pt(boost::bind<int>(f, 1, 2));  
    boost::unique_future<int> fi = pt.get_future();  
    boost::thread th2(boost::move(pt));  
    fi.wait();  
    th2.join();  
    cout <<"x+y="<<fi.get()<<endl;  

    boost::packaged_task<int> pt_cls(boost::bind<int>(&Class::add, &cls, 1, 2));  
    boost::unique_future<int> fi_cls = pt_cls.get_future();  
    boost::thread th2_cls(boost::move(pt_cls));  
    fi_cls.wait();  
    th2_cls.join();  
    cout <<"x+y="<<fi_cls.get()<<endl;  

    /* 
     *related functions 
     *  boost::thread 
     *  boost::function 
     *  boost::bind 
     */  

    //some usages:  
    int x = 1, y =2;  
    cout << "x+y=" <<boost::bind<int>(f, _1, _2)(x,y)<<endl;  

    system("pause");
    return 0;  
}  

boost_tutorial

相关文章:

  • 快速搭建个人博客
  • 一文读懂ML中的解析解与数值解
  • java B2B2C Springcloud电子商务平台源码 -Feign之源码解析
  • 转: Genymotion使用及离线镜像的安装
  • 全面解析JavaScript的对象创建和继承
  • 数据库出现锁表情况
  • 正式推出市场的前兆?波士顿动力机器狗上演「徒手」拉卡车
  • 浅谈网络工程行业
  • IPFS环境搭建节点安装
  • Mac 系统安装robot framework
  • 前端跨域的解决方案
  • Hadoop所支持的几种压缩格式
  • mysql
  • Spring 依赖注入
  • 重定向
  • $translatePartialLoader加载失败及解决方式
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • [译]CSS 居中(Center)方法大合集
  • 《剑指offer》分解让复杂问题更简单
  • ComponentOne 2017 V2版本正式发布
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • Docker容器管理
  • exports和module.exports
  • Git初体验
  • idea + plantuml 画流程图
  • js数组之filter
  • MySQL几个简单SQL的优化
  • Spring框架之我见(三)——IOC、AOP
  • Vue ES6 Jade Scss Webpack Gulp
  • 从零开始学习部署
  • 基于遗传算法的优化问题求解
  • 目录与文件属性:编写ls
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 删除表内多余的重复数据
  • 微信小程序填坑清单
  • 我是如何设计 Upload 上传组件的
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 应用生命周期终极 DevOps 工具包
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • Android开发者必备:推荐一款助力开发的开源APP
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • ​Java并发新构件之Exchanger
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • #laravel 通过手动安装依赖PHPExcel#
  • #考研#计算机文化知识1(局域网及网络互联)
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (pojstep1.1.2)2654(直叙式模拟)
  • (rabbitmq的高级特性)消息可靠性
  • (一)Dubbo快速入门、介绍、使用
  • (一)VirtualBox安装增强功能
  • (转) ns2/nam与nam实现相关的文件
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)Linq学习笔记
  • ./和../以及/和~之间的区别