POCO c++ 使用例子
.定时器
#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(, ); // fire after 250ms, repeat every 500ms
timer.start(TimerCallback<TimerExample>(te, &TimerExample::onTimer));
Thread::sleep();
timer.stop();
return ;
}
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;
}
.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 ;
}
.内存池
#include "Poco/MemoryPool.h"
#include <string>
#include <iostream>
using Poco::MemoryPool;
int main(int argc, char** argv)
{
MemoryPool pool(); // 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 ;
}
.任务
#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 = ; i < ; ++i)
{
setProgress(float(i)/); // report progress
if (sleep())
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 ;
}
.通知
#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 ;
}
.线程
#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 ;
}
.线程对象
#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();
}
}
private:
Poco::Activity<ActivityExample> _activity;
}; int main(int argc, char** argv)
{
ActivityExample example;
example.start();
Thread::sleep();
example.stop();
return ;
}
.异步通知 #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 = ; i < ; ++i)
{
queue.enqueueNotification(new WorkNotification(i));
}
while (!queue.empty()) // wait until all work is done
Poco::Thread::sleep();
queue.wakeUpAll(); // tell workers they're done
ThreadPool::defaultPool().joinAll();
return ;
}
POCO c++ 使用例子的更多相关文章
- Poco库网络模块例子解析1-------字典查询
Poco的网络模块在Poco::Net名字空间下定义 下面是字典例子解析 #include "Poco/Net/StreamSocket.h" //流式套接字 #include ...
- 进一步封装poco下的mysql操作
为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 主 ...
- PetaPoco入门(二)
1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...
- ORM之PetaPoco入门(二)--Petapoco基本用法
1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...
- PetaPoco入门
(转自:http://www.cnblogs.com/tinyhu/archive/2013/06/02/3113652.html) 1. ORM概括 1.1. ORM简介 ORM 对象-关系映射(O ...
- 如何在Android手机上进行自动化测试(下)
版权声明:允许转载,但转载必须保留原链接:请勿用作商业或者非法用途 前言 通过阅读本篇教程,你将会了解到: 如何使用Poco对Android原生应用进行测试 Poco支持直接对任何Android原生应 ...
- Poco C++库网络模块例子解析2-------HttpServer
//下面程序取自 Poco 库的Net模块例子----HTTPServer 下面开始解析代码 #include "Poco/Net/HTTPServer.h" //继承自TCPSe ...
- 学习懈怠的时候,可以运行Qt自带的Demo,或者Delphi控件自带的Demo,或者Cantu书带的源码,运行一下Boost的例子(搞C++不学习Boost/Poco/Folly绝对是一大损失,有需要使用库要第一时间想到)(在六大的痛苦经历说明,我的理论性确实不强,更适合做实践)
这样学还不用动脑子,而且熟悉控件也需要时间,而且慢慢就找到感觉了,就可以精神抖擞的恢复斗志干活了.或者Cantu书带的源码. 并且可以使用Mac SSD运行Qt的Demo,这样运行速度快一点. 此外, ...
- 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.
说明 为何要写这篇文章 ,之前看过阿二的梦想船的<Poco::TCPServer框架解析> http://www.cppblog.com/richbirdandy/archive/2010 ...
随机推荐
- Java代码Bug分析插件 FindBugs
http://www.oschina.net/p/findbugs FindBugs是一个能静态分析源代码中可能会出现Bug的Eclipse插件工具.
- 基于Linux的智能家居的设计(5)
4 软件设计 软件设计部分主要包含uboot移植.内核编译.系统移植.设备驱动编程.应用程序编程(QT编程.mysql数据库编程.控制系统编程).各个模块的功能函数(部分是在windows以下的IA ...
- 141. Sqrt(x) 【easy】
141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt ...
- Consul实现原理系列文章2: 用Gossip来做集群成员管理和消息广播
工作中用到了Consul来做服务发现,之后一段时间里,我会陆续发一些文章来讲述Consul实现原理.这篇文章会讲述Consul是如何使用Gossip来做集群成员管理和消息广播的. Consul使用Go ...
- 我的第二个java程序 循环
public class Test {//类 public Test (int num){//构造方法,和类同名,无返回值,接收传参并定义传参的类型,大小写敏感 int x = 10;//局部变量,定 ...
- servelet 连接mysql
package helloworld; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; impor ...
- 第一百八十五节,jQuery,Ajax 表单插件
jQuery,Ajax 表单插件 学习要点: 1.核心方法 2.option 参数 3.工具方法 传统的表单提交,需要多次跳转页面,极大的消耗资源也缺乏良好的用户体验.而这款 form.js 表单的 ...
- 音频处理之去噪算法---基于pcm和g711的音频16000hz、8bit去噪声算法
(1)应用背景 (2)主要降噪算法原理 (3)算法流程 (4)算法实现 (5) ------------author:pkf -------------------time:2-6 --------- ...
- 浅谈IM软件怎样建立安全socket连接、登录
----------------------------------------------------欢迎查看IM软件业务知识<专栏>-------------------------- ...
- unity3d面试题与参考答案
1.C#程序题 1 2 3 4 5 6 7 8 9 10 11 private static void aaa(int x) { x = 10; } private static void bbb ...