2013-01-10 23 views
5

以下のテストコードでスレッドセーフなようです。マルチスレッドプログラムでPoco::Loggerを使用できますか?ここPoco :: Loggerはスレッドセーフですか?

static Poco::Logger *pLogger;  
class MyRunnable : public Poco::Runnable { 
    private: 
     std::string _name; 
     Poco::Random _rnd; 
    public: 
     void setName(std::string name) { 
      _name = name; 
     } 
     void run() { 
     for (int i=0; i<200; i++) { 
      pLogger->information("info from: " + _name); 
      _rnd.seed(_rnd.next(65532) * _name.size()); 
      Poco::Thread::sleep(_rnd.next(13) + 1); 
     } 
     } 
}; 

テストメインです:

int 
main (int argc, char *argv[]) 
{ 
    Poco::Thread thr1, thr2, thr3; 
    MyRunnable *pMyR1 = new MyRunnable(), 
       *pMyR2 = new MyRunnable(), 
       *pMyR3 = new MyRunnable(); 
    pMyR1->setName("r1"); 
    pMyR2->setName("ra2"); 
    pMyR3->setName("runable3"); 

    Poco::FormattingChannel *pFCFile = new Poco::FormattingChannel(new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s: %q:%t")); 
    pFCFile->setChannel(new Poco::FileChannel("test.log")); 
    pFCFile->open(); 
    pLogger = &(Poco::Logger::create("FileLogger", pFCFile, Poco::Message::PRIO_INFORMATION)); 


    thr1.start(*pMyR1); 
    thr2.start(*pMyR2); 
    thr3.start(*pMyR3); 

    std::cout << "starting..." << std::endl; 
    thr1.join(); 
    thr2.join(); 
    thr3.join(); 
    std::cout << "end." << std::endl; 
    return EXIT_SUCCESS; 
}   /* ---------- end of function main ---------- */ 
+2

[このページ](http://www.appinf.com/docs/poco/Poco.Logger.html)は、 'unsafeGet'はスレッドセーフではないとしか言​​いませんので、残りの部分は想定しています。 – chris

+0

一般に、明示的に指定されない限り、スレッドセーフではないという機能性を常に考慮する必要があります。 –

答えて

9

この質問は非常に古いですが、私は私が見つけたライブラリフォーラムで見て、同じ疑問を持っていた:重要な引用がある http://pocoproject.org/forum/viewtopic.php?f=12&t=1233&p=2681&hilit=logger#p2681
:他のスレッドが現在Loggerを使用している間にLoggerに接続されているChannelを変更しようとすると、問題が発生する可能性があります。

+0

リンクを投稿するだけでなく、要約してください。 –

+0

今日の見積もりは: "ロガーは異なるロギング機能に関してスレッドセーフです。別のスレッドが現在ロガーを使用している間にロガーに接続されたチャンネルを変更しようとすると、問題が発生する可能性があります。 –

+0

ありがとう、upvoted。投稿を修正する必要があります(コメントとして残しておく)。 –

関連する問題