boost教程
来源:互联网 发布:化学软件 编辑:程序博客网 时间:2024/05/17 01:05
由于项目需要,最近在学习boost 类库,在github上创建了一个boost_tutorial项目,欢迎有兴趣的同学丰富。
boost::thread 的用法如下,更多项目请移步boost_tutorial,其它将在github上陆续更新:
- /*
- * boost::thread tutorial
- *
- * zdd / zddhub@gmail.com
- * https://github.com/zddhub/boost_tutorial
- */
- #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;
- return 0;
- }
阅读全文
1 0
- boost教程
- boost教程
- Boost.Signals 教程
- Boost教程网址链接
- Boost的UDP教程
- Boost正则表达式教程
- boost线程池教程
- boost -- signals 教程翻译
- boost教程网址
- boost filesystem 教程
- boost signal2 教程
- boost安装详细教程
- Boost.Asio教程 0
- Boost.Aiso教程 1
- Boost.Aiso教程 2
- Boost.Aiso教程 3
- boost regex 超详细教程
- boost regex 超详细教程
- Nginx查看并发链接数
- Objective-C中的@dynamic
- 网络七层模型&&网络数据包
- C++菱形继承和菱形虚拟继承
- 20170725
- boost教程
- SQL(1)
- *畅通工程续
- HDU 1874 Dijkstra (迪杰斯特拉)(队列及邻接矩阵)
- 一步一步教你使用uCOS-II
- linux 命令
- Canvas 中 concat 与 setMatrix
- 《Maven-编译错误解决》---请使用 -Xlint:unchecked 重新编译
- 获取特殊路径