2013-01-10 2 views
5

아래의 테스트 코드에서는 threadsafe와 비슷합니다. 멀티 스레드 프로그램에서 Poco::Logger을 사용할 수 있습니까? 여기 Poco :: Logger는 threadsafe입니까?

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
중요한 인용은 다음과 같습니다 "로거는 다른 로깅 기능과 관련하여 스레드로부터 안전합니다. 다른 스레드가 현재 로거를 사용하고있는 동안 로거에 연결된 채널을 변경하려고하면 문제가 발생할 수 있습니다."

+0

링크를 게시하는 대신 요약하십시오. –

+0

오늘의 인용문은 "다른 로깅 기능과 관련하여 로거는 안전합니다. 다른 스레드가 현재 로거를 사용하고있는 동안 로거에 연결된 채널을 변경하려고하면 문제가 발생할 수 있습니다." –

+0

고마워, upvoted. 게시물을 수정해야합니다 (의견을 남기지 않고). –