1.定时器
#include"Poco/Timer.h"#include"Poco/Thread.h"
usingPoco::Timer;usingPoco::TimerCallback;classTimerExample
{public:void onTimer(Poco::Timer&timer)
{
std::cout<< "onTimer called." <<:endl>
}
};int main(int argc, char**argv)
{
TimerExample te;
Timer timer(250, 500); //fire after 250ms, repeat every 500ms
timer.start(TimerCallback(te, &TimerExample::onTimer));
Thread::sleep(5000);
timer.stop();return 0;
}
2.管道
#include "Poco/Process.h"
#include "Poco/PipeStream.h"
#include "Poco/StreamCopier.h"
#include
using Poco::Process;
using Poco::ProcessHandle;
int main(int argc, char** argv)
{
std::string cmd("/bin/ps");
std::vector<: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
class HelloRunnable: publicPoco::Runnable
{virtual voidrun()
{
std::cout<< "Hello, bingzhe" <<: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#include
usingPoco::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(pool.get());
std::cin.read(buffer, pool.blockSize());
std::streamsize n=std::cin.gcount();
std::strings(buffer, n);
pool.release(buffer);
std::cout<< s <<:endl>
}
5.任务
#include"Poco/Task.h"#include"Poco/TaskManager.h"#include"Poco/TaskNotification.h"#include"Poco/Observer.h"
usingPoco::Observer;class SampleTask: publicPoco::Task
{public:
SampleTask(const std::string&name): Task(name)
{}voidrunTask()
{for (int i = 0; i < 100; ++i)
{
setProgress(float(i)/100); //report progress
if (sleep(1000))break;
}
}
};classProgressHandler
{public:void onProgress(Poco::TaskProgressNotification*pNf)
{
std::cout<< pNf->task()->name()<< "progress:" << pNf->progress() <<:endl>
pNf->release();
}void onFinished(Poco::TaskFinishedNotification*pNf)
{
std::cout<< pNf->task()->name() << "finished." <<:endl>
pNf->release();
}
};int main(int argc, char**argv)
{
Poco::TaskManager tm;
ProgressHandler pm;
tm.addObserver(
Observer(pm,&ProgressHandler::onProgress)
);
tm.addObserver(
Observer(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
usingPoco::NotificationCenter;usingPoco::Notification;usingPoco::Observer;usingPoco::NObserver;usingPoco::AutoPtr;class BaseNotification: publicNotification
{public: voiddosome(){
printf("fuck!");
}
};class SubNotification: publicBaseNotification
{
};classTarget
{public:void handleBase(BaseNotification*pNf)
{
std::cout<< "handleBase:" << pNf->name() <<:endl>
pNf->dosome();
pNf->release(); //we got ownership, so we must release
}void handleSub(const AutoPtr&pNf)
{
std::cout<< "handleSub:" << pNf->name() <<:endl>
}
};int main(int argc, char**argv)
{
NotificationCenter nc;
Target target;
nc.addObserver(
Observer(target, &Target::handleBase)
);
nc.addObserver(
NObserver(target, &Target::handleSub)
);
nc.postNotification(newBaseNotification);
nc.postNotification(newSubNotification);
nc.removeObserver(
Observer(target, &Target::handleBase)
);
nc.removeObserver(
NObserver(target, &Target::handleSub)
);return 0;
}
7.线程
#include"Poco/Thread.h"#include"Poco/Runnable.h"#include
class HelloRunnable: publicPoco::Runnable
{virtual voidrun()
{
std::cout<< "Hello, bingzhe!" <<: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
usingPoco::Thread;classActivityExample
{public:
ActivityExample(): _activity(this,&ActivityExample::runActivity)
{}voidstart()
{
_activity.start();
}voidstop()
{
_activity.stop();//request stop
_activity.wait(); //wait until activity actually stops
}protected:voidrunActivity()
{while (!_activity.isStopped())
{
std::cout<< "bingzhe running." <<:endl>
Thread::sleep(200);
}
}private:
Poco::Activity_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"
usingPoco::Notification;usingPoco::NotificationQueue;usingPoco::ThreadPool;usingPoco::Runnable;usingPoco::AutoPtr;class WorkNotification: publicNotification
{public:
WorkNotification(intdata): _data(data) {}int data() const{return_data;
}private:int_data;
};class Worker: publicRunnable
{public:
Worker(NotificationQueue&queue): _queue(queue) {}voidrun()
{
AutoPtrpNf(_queue.waitDequeueNotification());while(pNf)
{
WorkNotification* pWorkNf =dynamic_cast(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(newWorkNotification(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;
}