Aim
Sometime we want to attach a thread to a class. This example used Linux default thread. This example is Linux dependent. The basic idea is that once we create the class we let the thread loop in a member function.
Source
#include <iostream> #include <thread> #include <chrono> #include "ClassWithThread.h" #include "Logger.h" int main(int argc, char *argv[]) { Logger::LoggerConfig config; config.logLevel = Logger::LogLevel::DEBUG; config.logPath = "."; config.fileSize = 0; config.fileSizeLimit = 4 * 1024 * 1024; // 4 MByte config.isToConsole = true; config.isToFile = false; LOGMSG_INIT(config); Trader cc; cc.InitComponent(); std::this_thread::sleep_for(std::chrono::seconds(1)); return 0; }
Output
$ ./src/LinuxThread 20200501_210847_578 [MSG] Trader:: Main: 24, 18881,IN 20200501_210848_578 [MSG] Trader:: Main: 31, 18881,OUT
沒有留言:
發佈留言