
プログラミングをしていて、使えるパターンにWorkerThreadパターンがあります。
処理をRequestとしてChannelに登録して順次処理してもらいます。
C++で上記の処理を実現したい時に、Pocoフレームワークを使うと簡単に実現する事ができます。
クラス | 説明 |
---|---|
Channel | 処理を受け付けるクラス |
Request | 処理を表す、サブクラスで処理を定義する |
Worker | 実際に処理を行うクラス |
使い方は以下の感じです。ソースコードはGithubをご覧ください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include "request.h" #include "channel.h" #include <iostream> #include <Poco/Thread.h> using namespace htn; class PrintRequest : public Request { public : PrintRequest( int index) :index_(index) { }; ~PrintRequest() {}; virtual void Execute() { int val = Poco::Thread::currentTid(); { std::cout << "Execute [" << std::to_string(val) << "]" << std::to_string(index_) << std::endl; } Poco::Thread::sleep(100); }; int index_; }; int main( int argc, char const * argv[]) { std::cout << "hello world" << std::endl; { Channel channel(3); for ( int i = 0; i < 100; i++) { channel.PutRequest( new PrintRequest(i)); } Poco::Thread::sleep(10 * 1000); } return 0; } |
0 件のコメント :
コメントを投稿