我正在编写一个记录消息的函数。我将从不同的线程调用这个打印函数。我的代码如下:
MyLog::printLog(const char* s)
{
std::string myline(s);
//m_Mutex is class member and there will be only object for this class
// shared by all threads
int ret = pthread_mutex_lock(&m_Mutex);
if ( ret != 0 )
{
std::cout<<" trying to lock same mutex char* "<<std::endl;
}
//code to log message in File
pthread_mutex_unlock(&m_Mutex);
}
我的问题是,如果上面的函数是从不同的线程调用的,参数是 "from thread1" , "from thread 2" ,......是否有可能 const char *s 会混淆打印错误的值。?我希望我的问题很清楚。