首页 > 代码库 > POCO c++ 使用例子
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; }
POCO c++ 使用例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。