【发布时间】:2023-03-25 01:09:01
【问题描述】:
我正在编写 C API 的“线程安全”C++ 包装器,而 API 本身不是内部线程安全的。我尝试过使用 RAII。
我想知道,我的实现是否正确?以及它是否是线程安全的。我感谢我的代码中的任何 cmets。提前致谢!
要包装的C API如下,
/* an data structure which represents a connection proxy to the logger: */
struct cLog_Logger;
/* connect the logger, and returns a handle to it: */
cLog_Logger* cLog_connect();
/* appends a zero terminated string to the log: */
void cLog_write(cLog_Logger* logger, const char* message);
/* closes the connection with the logger: */
void cLog_close(cLog_Logger* logger);
我的包装器实现如下:
class LoggerWrapper{
public:
LoggerWrapper(){ //constructor
cLog= cLog_connect();
}
void log(const std::string &message){ //entry point
cLog_write(cLog, message);
cLog_close(cLog);
}
~LoggerWrapper(){ //destructor
delete cLog;
}
protected:
cLog_Logger *cLog;
}
谢谢!
【问题讨论】:
-
线程在哪里?
-
@Ajay,线程在包装器之外无处不在:)
标签: c++ thread-safety wrapper