POCO c++ 使用例子
1.定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using Poco::TimerCallback; class TimerExample { public: void onTimer(Poco::Timer& timer) { std::cout << "onTimer called." << std::endl; } }; int main(int argc, char** argv) { TimerExample te; Timer timer(250, 500); // fire after 250ms, repeat every 500ms timer.start(TimerCallback<TimerExample>(te, &TimerExample::onTimer)); Thread::sleep(5000); timer.stop(); return 0; }
2.管道#include "Poco/Process.h" #include "Poco/PipeStream.h" #include "Poco/StreamCopier.h" #include <fstream> using Poco::Process; using Poco::ProcessHandle; int main(int argc, char** argv) { std::string cmd("/bin/ps"); std::vector<std::string> args; args.push_back("-ax"); Poco::Pipe outPipe; ProcessHandle ph = Process::launch(cmd, args, 0, &outPipe, 0); Poco::PipeInputStream istr(outPipe); std::ofstream ostr("processes.txt"); Poco::StreamCopier::copyStream(istr, ostr); return 0; }
3.poco 默认线程池 #include "stdafx.h" #include "Poco/ThreadPool.h" #include "Poco/Runnable.h" #include <iostream> class HelloRunnable: public Poco::Runnable {virtual void run(){std::cout << "Hello, bingzhe" << std::endl;} }; int main(int argc, char** argv) {HelloRunnable runnable;Poco::ThreadPool::defaultPool().start(runnable);Poco::ThreadPool::defaultPool().joinAll();return 0; }
4.内存池#include "Poco/MemoryPool.h" #include <string> #include <iostream> using Poco::MemoryPool; int main(int argc, char** argv) { MemoryPool pool(1024); // unlimited number of 1024 byte blocks // MemoryPool pool(1024, 4, 16); // at most 16 blocks; 4 preallocated char* buffer = reinterpret_cast<char*>(pool.get()); std::cin.read(buffer, pool.blockSize()); std::streamsize n = std::cin.gcount(); std::string s(buffer, n); pool.release(buffer); std::cout << s << std::endl; return 0; }
5.任务 #include "Poco/Task.h" #include "Poco/TaskManager.h" #include "Poco/TaskNotification.h" #include "Poco/Observer.h" using Poco::Observer; class SampleTask: public Poco::Task { public: SampleTask(const std::string& name): Task(name) {} void runTask() { for (int i = 0; i < 100; ++i) { setProgress(float(i)/100); // report progress if (sleep(1000)) break; } } }; class ProgressHandler { public: void onProgress(Poco::TaskProgressNotification* pNf) { std::cout << pNf->task()->name() << " progress: " << pNf->progress() << std::endl; pNf->release(); } void onFinished(Poco::TaskFinishedNotification* pNf) { std::cout << pNf->task()->name() << " finished." << std::endl; pNf->release(); } }; int main(int argc, char** argv) { Poco::TaskManager tm; ProgressHandler pm; tm.addObserver( Observer<ProgressHandler, Poco::TaskProgressNotification> (pm, &ProgressHandler::onProgress) ); tm.addObserver( Observer<ProgressHandler, Poco::TaskFinishedNotification> (pm, &ProgressHandler::onFinished) ); tm.start(new SampleTask("Task 1")); // tm takes ownership tm.start(new SampleTask("Task 2")); tm.joinAll(); return 0; }
6.通知 #include "stdafx.h"#include "Poco/NotificationCenter.h" #include "Poco/Notification.h" #include "Poco/Observer.h" #include "Poco/NObserver.h" #include "Poco/AutoPtr.h" #include <iostream> using Poco::NotificationCenter; using Poco::Notification; using Poco::Observer; using Poco::NObserver; using Poco::AutoPtr; class BaseNotification: public Notification { public: void dosome(){printf("fuck!");}}; class SubNotification: public BaseNotification { }; class Target { public: void handleBase(BaseNotification* pNf) { std::cout << "handleBase: " << pNf->name() << std::endl; pNf->dosome();pNf->release(); // we got ownership, so we must release } void handleSub(const AutoPtr<SubNotification>& pNf) { std::cout << "handleSub: " << pNf->name() << std::endl; } }; int main(int argc, char** argv) { NotificationCenter nc; Target target; nc.addObserver( Observer<Target, BaseNotification>(target, &Target::handleBase) ); nc.addObserver( NObserver<Target, SubNotification>(target, &Target::handleSub) ); nc.postNotification(new BaseNotification); nc.postNotification(new SubNotification); nc.removeObserver( Observer<Target, BaseNotification>(target, &Target::handleBase) ); nc.removeObserver( NObserver<Target, SubNotification>(target, &Target::handleSub) ); return 0; }
7.线程#include "Poco/Thread.h" #include "Poco/Runnable.h" #include <iostream> class HelloRunnable: public Poco::Runnable { virtual void run() { std::cout << "Hello, bingzhe!" << std::endl; } }; int main(int argc, char** argv) { HelloRunnable runnable; Poco::Thread thread; thread.start(runnable); thread.join(); return 0; }
8.线程对象#include "Poco/Activity.h" #include "Poco/Thread.h" #include <iostream> using Poco::Thread; class ActivityExample { public: ActivityExample(): _activity(this, &ActivityExample::runActivity) {} void start() { _activity.start(); } void stop() { _activity.stop(); // request stop _activity.wait(); // wait until activity actually stops } protected: void runActivity() { while (!_activity.isStopped()) { std::cout << "bingzhe running." << std::endl; Thread::sleep(200); } } private: Poco::Activity<ActivityExample> _activity; }; int main(int argc, char** argv) { ActivityExample example; example.start(); Thread::sleep(2000); example.stop(); return 0; }
9.异步通知#include "stdafx.h"#include "Poco/Notification.h" #include "Poco/NotificationQueue.h" #include "Poco/ThreadPool.h" #include "Poco/Runnable.h" #include "Poco/AutoPtr.h" using Poco::Notification; using Poco::NotificationQueue; using Poco::ThreadPool; using Poco::Runnable; using Poco::AutoPtr; class WorkNotification: public Notification { public: WorkNotification(int data): _data(data) {} int data() const { return _data; } private: int _data; }; class Worker: public Runnable { public: Worker(NotificationQueue& queue): _queue(queue) {} void run() { AutoPtr<Notification> pNf(_queue.waitDequeueNotification()); while (pNf) { WorkNotification* pWorkNf = dynamic_cast<WorkNotification*>(pNf.get()); if (pWorkNf) { printf("hi!bingzhe");// Sleep(100); } pNf = _queue.waitDequeueNotification(); } } private: NotificationQueue& _queue; }; int main(int argc, char** argv) { NotificationQueue queue; Worker worker1(queue); // create worker threads Worker worker2(queue); ThreadPool::defaultPool().start(worker1); // start workers ThreadPool::defaultPool().start(worker2); // create some work for (int i = 0; i < 100; ++i) { queue.enqueueNotification(new WorkNotification(i)); } while (!queue.empty()) // wait until all work is done Poco::Thread::sleep(100); queue.wakeUpAll(); // tell workers they're done ThreadPool::defaultPool().joinAll(); return 0; }
转载于:https://www.cnblogs.com/xuandi/p/6427953.html
POCO c++ 使用例子相关推荐
- 【转】【翻译】实体框架中的POCO支持 - 第二部分 - 复杂类型,延迟装载和显式装载...
[原文地址]POCO in the Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading [ ...
- poco c++ 开发指南_掌握这个框架,你将会开发通杀全平台的万能爬虫
想开发网页爬虫,发现被反爬了?想对 App 抓包,发现数据被加密了?不要担心,使用 Airtest 开发 App 爬虫,只要人眼能看到,你就能抓到,最快只需要2分钟,兼容 Unity3D.Cocos2 ...
- C# 9 record 并非简单属性 POCO 的语法糖
最近升级专案到大统一 .NET 5 并使用 C#9 语法尝试改写套件,发现之前以为 record 只是简单属性 POCO 的简化语法糖的认知是错误. 另外因为 POCO 属于需定义口语词,这边在本文定 ...
- centos7 下安装Poco
参考: https://pocoproject.org/download.html 以下是我自己安装时的步骤,亲自实践! 1. 下载 git clone -b master https://githu ...
- POCO C++库导游【转】
原文:http://www.cnblogs.com/pocobo/archive/2008/12/29/1364699.html POCO C++库导游 Introduction A Guided T ...
- C#坏习惯:通过不好的例子学习如何制作好的代码——第2部分
目录 介绍 这篇文章的目标 Switch case与字典模式 第一个问题 第二个问题 更有力的例子 了解对象的生命周期 有用的字典 每次调用相同的实例 基础(Basic)版本 代码外的配置 在单独的源 ...
- POCO C++库学习和分析 -- 序
POCO C++库学习和分析 -- 序 1. POCO库概述: POCO是一个C++的开源库集.同一般的C++库相比,POCO的特点是提供了整一个应用框架.如果要做C++程序应用框架的快速开发,我觉得 ...
- POCO C++库学习和分析 -- 异常、错误处理、调试
POCO C++库学习和分析 -- 异常.错误处理.调试 1. 异常处理 C++同C语言相比,提供了异常机制.通过使用try,catch关键字可以捕获异常,这种机制使得程序员在程序异常发生时,可以通过 ...
- poco源码简单分析
自动化工具poco源码简单分析 Airtest简介 Airtest是网易游戏开源的一款UI自动化测试项目,目前处于公开测试阶段,该项目分为AirtestIDE.Airtest.Poco.Testlab ...
- poco不断重启?看这6点就够了
此文章来源于项目官方公众号:"AirtestProject" 版权声明:允许转载,但转载必须保留原链接:请勿用作商业或者非法用途 1. 前言 经常有同学会遇到poco不断重启的问题 ...
最新文章
- 【深度学习】——物体检测细节处理(NMS、样本不均衡、遮挡物体)
- python treeview底部加个按钮_Python爬取京东商品信息(GUI版本)
- 订单生产计划表范本_工厂生产管理为什么需要ERP软件?
- java 8 lambda 申明_2019-02-03——Java8 Lambda
- 联想小新/YOGA新品发布会官宣:定档10月20日
- OpenJudge 8782 乘积最大——S.B.S
- 逻辑运算map函数filter函数reduce函数
- linux启动mysql命令
- 阿里云ACE认证学习知识点梳理
- 恩格玛密码机的工作原理
- flink任务常见问题
- 锐龙7 7840U参数 r7 7840U性能怎么样 r77840U相当于什么水平
- 舆情、网络舆情、舆情分析
- FLStudio 四分音符八分音符 四四拍四二拍
- CSS—清除浮动的几种方式
- Mybatis的一对多映射
- Python matplotlib与tkinter结合
- PS制作麦克风质感图标,UI设计教程
- 采集HassCNC的设置
- ZMY_ImageLoader加载图片
热门文章
- 2017-2018-1 20155328 《信息安全系统设计基础》第13周学习总结
- Add NIC to Openfiler 2.3
- Android开发周报:Android 8.0开始推送、微店插件化实践
- linux下的raid及mdadm的命令详解
- linkin大话设计模式--单例模式
- 栈的应用实例——平衡符号
- 6425C-Lab5 管理计算机帐户
- asp.net 页面转向 Response.Redirect, Server.Transfer, Server.Execute的区别
- 如何理解操作系统的不确定性_如何创造可信任的机器学习模型?先要理解不确定性...
- 关于margin的数值是百分比,参照对象