【问题标题】:Is Poco::Logger threadsafe?Poco::Logger 线程安全吗?
【发布时间】:2012-12-24 09:37:36
【问题描述】:

在我下面的测试代码中似乎是线程安全的。我可以在多线程程序中使用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  ---------- */

【问题讨论】:

  • This page 只说unsafeGet 不是线程安全的,所以我认为其余的都是。
  • 一般来说,除非明确指定,否则您应该始终将功能视为不是线程安全的。

标签: c++ poco-libraries


【解决方案1】:

这个问题很老了,但我也有同样的疑问,所以在图书馆论坛上我发现: http://pocoproject.org/forum/viewtopic.php?f=12&t=1233&p=2681&hilit=logger#p2681
重要的引语是:“对于不同的日志记录功能,Logger 是线程安全的。如果您在另一个线程当前正在使用 Logger 时尝试更改连接到 Logger 的 Channel,这可能会导致问题。”

【讨论】:

  • 请总结链接而不是仅仅发布它们。
  • 所以当天的报价是:“Logger 对于不同的日志记录功能是线程安全的。如果您尝试更改连接到 Logger 的 Channel 而另一个线程当前正在使用 Logger,这可能导致问题。”
  • 谢谢,点赞。您应该修改您的帖子(而不是将其保留为评论)。
  • 感谢您的建议。完成!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 2011-06-07
  • 2023-03-04
  • 1970-01-01
  • 2020-08-30
  • 2020-04-15
  • 2011-07-04
相关资源
最近更新 更多