.定时器
#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 ;
}

最新文章

  1. MyEclipse、Eclipse复制web项目
  2. 如何通过SecureCRTPortable.exe 软件远程连接某个计算机(或者虚拟机)中的某个数据库
  3. Android九宫图(draw9patch)
  4. Linux面试基础题-2
  5. SSI框架中配置log4j
  6. 腾讯云(centos)上安装apache
  7. python学习第十一天 -- 函数式编程
  8. 利用VS自带的命令行工具查看和生产PublicKeyToken (转)
  9. MSSQL Server 2008 数据库安装失败
  10. Spring--注入类型--setter
  11. CAJ转换成PDF在线方法是什么
  12. 【转】Windows守护进程的一种简单实现
  13. 《算法》第一章部分程序 part 1
  14. 20155326 2006-2007-2 《Java程序设计》第4周学习总结
  15. 总有一些实用javascript的元素被人遗忘在角落-slice
  16. flutter初探
  17. PowerCLI
  18. jquery中prop()和attr()的区别
  19. python--内置常用模块(一) collections 队列 time模块 functiontools
  20. JVM虚拟机指令

热门文章

  1. 打开eclipse中文件所在文件夹
  2. POJ训练计划3096_Surprising Strings(STL/map)
  3. web页面性能分析一些网址
  4. javascript - Underscore 与 函数式编程
  5. Failed to import package with error: Couldn&#39;t decompress package
  6. poj 3666 河南省第七届程序设计D题(山区修路)
  7. Unity3d Serialize问题
  8. mysql之select into outfile
  9. Java对象类型的判断
  10. iOS 集成微信支付【转载】