2013-01-10 12 views
5

Sembra quasi sicuro nel mio codice di prova qui sotto. Posso usare Poco::Logger in un programma con multithreading?È Poco :: Logger sicuro?

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); 
     } 
     } 
}; 

qui è prova principale:

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

[Questa pagina] (http://www.appinf.com/docs/poco/Poco.Logger.html) dice solo che 'unsafeGet' non è sicuro da thread, quindi presumo che il resto lo sia. – chris

+0

Generalmente, se non specificato esplicitamente, si dovrebbe sempre considerare la funzionalità come _non_ essendo thread-safe. –

risposta

9

Questa domanda è molto vecchio, ma ho avuto lo stesso dubbio, in modo da guardare sul Forum biblioteca ho trovato: http://pocoproject.org/forum/viewtopic.php?f=12&t=1233&p=2681&hilit=logger#p2681
La citazione importante è: "Il logger è thread-safe per quanto riguarda la diversa funzione di logging.Se si prova a cambiare il canale collegato ad un logger mentre un altro thread sta attualmente utilizzando il logger, questo potrebbe portare a problemi."

+0

Riassumi i link invece di pubblicarli. –

+0

Quindi la quotazione del giorno è: "Il logger è thread-safe per quanto riguarda la diversa funzione di logging.Se provi a cambiare il canale collegato a un logger mentre un altro thread sta usando il logger, questo potrebbe causare problemi." –

+0

Grazie, potenziato. Dovresti modificare il tuo post (vs lasciandolo come commento). –