如何管理singleton类中的变量状态? 我尝试在Linux/UNIX环境下C++编写应用程序记录器程序,有效地处理多线程环境。我目前面临的问题与singleton类有关,请允许我先向您展示代码,然后我将询问我最近几天挖掘的Q- class Logger { private: int mNumber; public: static Logger& getInstance(int num){ static Logger object; /* I have already solved the problem for single threaded application, below is what I was doing */ object.setNumber(num); /* But I can not do the above in multi thread application, even with lock( I prefer pthread) mutexes and semaphores. */ return object; } void debug(const char* str){ std::cout << "Num : " << mNumber << " :: Message : " << str << std::endl; } private: void setNumber(const int num){ this->mNumber = value; } }; #define logMe Logger::getInstance(__LINE__) void* threadOne(void* args){ while(true){ logMe.debug("I am from threadOne"); } return (void*) nullptr; }// end int main(int argc, char** argv){ logMe.debug("Works with single threaded application."); /* 1) Correct me if I am wrong, the above gets expand to Logger::getInstance(__LINE__).debug("value"); 2) Now that is the problem, somehow, I want this value to pass to debug method. */ // This is what I have been trying to do- pthread_t tid; pthread_create(&tid, nullptr, threadOne, nullptr); while (true){ logMe.debug("I am from Main"); usleep(2000); // This is not neccesarry just to check while debugging. } exit (EXIT_SUCCESS); }// end 类记录器{ 私人: 整数; 公众: 静态记录器和getInstance(int num){ 静态记录器对象; /* 我已经解决了单线程应用程序的问题,下面是我正在做的 */ object.setNumber(num); /* 但在多线程应用程序中,即使使用锁(我更喜欢pthread)互斥体和信号量,我也无法实现上述功能。 */ 返回对象; } 无效调试(常量字符*str){ std::cout

如何管理singleton类中的变量状态? 我尝试在Linux/UNIX环境下C++编写应用程序记录器程序,有效地处理多线程环境。我目前面临的问题与singleton类有关,请允许我先向您展示代码,然后我将询问我最近几天挖掘的Q- class Logger { private: int mNumber; public: static Logger& getInstance(int num){ static Logger object; /* I have already solved the problem for single threaded application, below is what I was doing */ object.setNumber(num); /* But I can not do the above in multi thread application, even with lock( I prefer pthread) mutexes and semaphores. */ return object; } void debug(const char* str){ std::cout << "Num : " << mNumber << " :: Message : " << str << std::endl; } private: void setNumber(const int num){ this->mNumber = value; } }; #define logMe Logger::getInstance(__LINE__) void* threadOne(void* args){ while(true){ logMe.debug("I am from threadOne"); } return (void*) nullptr; }// end int main(int argc, char** argv){ logMe.debug("Works with single threaded application."); /* 1) Correct me if I am wrong, the above gets expand to Logger::getInstance(__LINE__).debug("value"); 2) Now that is the problem, somehow, I want this value to pass to debug method. */ // This is what I have been trying to do- pthread_t tid; pthread_create(&tid, nullptr, threadOne, nullptr); while (true){ logMe.debug("I am from Main"); usleep(2000); // This is not neccesarry just to check while debugging. } exit (EXIT_SUCCESS); }// end 类记录器{ 私人: 整数; 公众: 静态记录器和getInstance(int num){ 静态记录器对象; /* 我已经解决了单线程应用程序的问题,下面是我正在做的 */ object.setNumber(num); /* 但在多线程应用程序中,即使使用锁(我更喜欢pthread)互斥体和信号量,我也无法实现上述功能。 */ 返回对象; } 无效调试(常量字符*str){ std::cout,c++,multithreading,c++11,c++14,C++,Multithreading,C++11,C++14,将执行工作的类与保存值的类分开: struct Logger { struct Line { Logger &log; int n; void debug(const char *s) {log.debug(n,s);} }; Line at(int n) {return {*this,n};} private: void debug(int n,const char *s) {std::cout << "Num : "

将执行工作的类与保存值的类分开:

struct Logger {
  struct Line {
    Logger &log;
    int n;
    void debug(const char *s)
    {log.debug(n,s);}
  };

  Line at(int n) {return {*this,n};}

private:
  void debug(int n,const char *s)
  {std::cout << "Num : "  << n << " :: Message : " << s << std::endl;}
};
struct记录器{
结构线{
记录器和日志;
int n;
无效调试(常量字符*s)
{log.debug(n,s);}
};
(int n){return{*this,n};}处的行
私人:
无效调试(int n,const char*s)

{std::cout如何
#定义logMe(s)Logger::getInstance().debug(uu LINE,s)
并让debug将行号作为参数?我不是在讨论这是否是做日志的最佳方法,甚至是正确方法,但为了解决您的问题,只需调用log as
logMe(“我来自Main”)
并将
logMe
定义为:
#定义logMe(x)Logger::getInstance(uuu行)。调试(std::string((x))+u行)
,或者类似的方法参数从一开始就保留了选项。但比方说,它赋予用户显式更改参数值的权力。你知道我的意思。@EljayLet me check@Amadeus。谢谢。我想不是,我也过载了