2013-01-10 42 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

[This page](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時嘗試更改連接到Logger的Channel,這可能會導致問題。」

+0

請總結鏈接,而不是隻發佈它們。 –

+0

因此,當天的報價是: 「Logger對於不同的日誌記錄功能是線程安全的,如果在另一個線程正在使用Logger時嘗試更改連接到Logger的Channel,這可能會導致問題。 –

+0

謝謝,upvoted。你應該修改你的帖子(與留下它作爲評論)。 –