.定时器
#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++ 使用例子的更多相关文章

  1. Poco库网络模块例子解析1-------字典查询

    Poco的网络模块在Poco::Net名字空间下定义   下面是字典例子解析 #include "Poco/Net/StreamSocket.h" //流式套接字 #include ...

  2. 进一步封装poco下的mysql操作

    为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 主 ...

  3. PetaPoco入门(二)

    1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...

  4. ORM之PetaPoco入门(二)--Petapoco基本用法

    1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...

  5. PetaPoco入门

    (转自:http://www.cnblogs.com/tinyhu/archive/2013/06/02/3113652.html) 1. ORM概括 1.1. ORM简介 ORM 对象-关系映射(O ...

  6. 如何在Android手机上进行自动化测试(下)

    版权声明:允许转载,但转载必须保留原链接:请勿用作商业或者非法用途 前言 通过阅读本篇教程,你将会了解到: 如何使用Poco对Android原生应用进行测试 Poco支持直接对任何Android原生应 ...

  7. Poco C++库网络模块例子解析2-------HttpServer

    //下面程序取自 Poco 库的Net模块例子----HTTPServer 下面开始解析代码 #include "Poco/Net/HTTPServer.h" //继承自TCPSe ...

  8. 学习懈怠的时候,可以运行Qt自带的Demo,或者Delphi控件自带的Demo,或者Cantu书带的源码,运行一下Boost的例子(搞C++不学习Boost/Poco/Folly绝对是一大损失,有需要使用库要第一时间想到)(在六大的痛苦经历说明,我的理论性确实不强,更适合做实践)

    这样学还不用动脑子,而且熟悉控件也需要时间,而且慢慢就找到感觉了,就可以精神抖擞的恢复斗志干活了.或者Cantu书带的源码. 并且可以使用Mac SSD运行Qt的Demo,这样运行速度快一点. 此外, ...

  9. 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.

    说明 为何要写这篇文章 ,之前看过阿二的梦想船的<Poco::TCPServer框架解析> http://www.cppblog.com/richbirdandy/archive/2010 ...

随机推荐

  1. Maven 命令行创建项目时 Could not find goal ‘create’ in plugin org.apache.maven.plugins:...

    使用maven3.3.9 版本,进行命令行创建项目时输入以下命令创建失败 mvn archetype:create -DgroupId=com.zang.maven  -DartifactId=sys ...

  2. WP8滑动条(Slider)控件的使用

    1. <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinit ...

  3. 虚拟机安装Ubuntu 12.04 出现提示“Ubuntu is running in low-graphics mode?”

    原文链接: http://blog.csdn.net/maimang1001/article/details/17048273 http://blog.csdn.net/bluetropic/arti ...

  4. 锁定窗口,禁止更新的win32函数

    [DllImport("user32.dll", EntryPoint = "LockWindowUpdate", SetLastError = true, E ...

  5. nginx php 使用unix socket 还是tcp?

    两种通信方式的分析和总结 从原理上来说,unix socket方式肯定要比tcp的方式快而且消耗资源少,因为socket之间在nginx和php-fpm的进程之间通信,而tcp需要经过本地回环驱动,还 ...

  6. MTU的概念,什么是路径MTU? MTU发现机制,TraceRoute(了解)

    1.MTU的概念      MTU即Maximum Transmission Unit 最大传输单元.它是指一种通信协议的某一层上面所能通过的最大数据包大小(以字节为单位). 2.路径MTU     ...

  7. Linux下(centos6.8)JDK1.8的安装与配置

    今天说下在Linux(centos6.8)系统下的JDK安装与配置. 据我所知的jdk安装方式有三种(rpm.yum方式没用过,暂且不提)今天只说解压安装方式: 一.解压jdk安装包: 附上jdk1. ...

  8. 调整Redmine的用户显示格式

    在 Redmine 中新建用户时是酱紫的: 必须指定姓氏.名字.然后 Redmine 默认是按"名字 姓氏"这样的方式显示用户.比方"张三",会显示成" ...

  9. gcc -M -MM -MQ -MF -MT -MD

    静态模式规则对一个较大工程的管理非常有用.它可以对整个工程的同一类文件的重建规则进行一次定义,而实现对整个工程中此类文件指定相同的重建规则.比如,可以用来描述整个工程中所有的.o 文件的依赖规则和编译 ...

  10. 查看zookeeper是否启动

    查看进程: 方法: ps -aux | grep 'zookeeper' 系统有返回,说明zookeeper启动. 你可以搜索ps -aux.