c++ - 当 std::lock_guard 仍在范围内时,使用 pthread_create 创建线程是否安全?
问题描述
我有一个类似下面的函数,其中线程通过使用std::lock_guard
互斥锁获取锁并通过ofstream
.
当当前文件大小增加最大大小时,我想创建一个应该压缩文件并终止的独立线程。
我想了解调用pthread_create
whenstd::lock_guard
仍在范围内的含义。
安全吗?锁是否也会应用于新线程(我不打算这样)?
void log (std::string message)
{
std::lock_guard<std::mutex> lck(mtx);
_outputFile << message << std::endl;
_outputFile.flush();
_sequence_number++;
_curr_file_size = _outputFile.tellp();
if (_curr_file_size >= max_size) {
char *lf = strdup(_logfile.c_str());
// Create an independent thread to compress the file since
// it takes some time to compress huge files.
if (!_compress_thread) {
pthread_create(&_compress_thread, NULL, compress_log, (void *)lf);
}
}
}
void * compress_log (void *arg)
{
pthread_detach(pthread_self());
// Code to compress the file
// ...
{ // Create a scope for lock_gaurd
std::lock_guard<std::mutex> lck(mtx);
_compress_thread = NULL;
}
pthread_exit(NULL);
}
解决方案
互斥体在线程级别工作,它只影响使用它的线程。当一个线程锁定一个互斥体时,会发生两件事:
- 互斥锁被解锁 - 它被锁定并且线程继续执行。
- 互斥体已被锁定 - 线程不会继续,而是等待互斥体解锁。
您的新线程运行该compress_log()
函数,该函数根本不访问互斥锁。因此,无论互斥锁是否被锁定,它都会运行(在您的情况下,互斥锁将在log()
退出时解锁)。
一个不相关的建议:使用std::thread
而不是pthread_create
,这样您的应用程序变得更加可移植:
std::thread{ [lf] { compress_log(lf); } }.detach();
推荐阅读
- python-3.x - 如何将数据列表(括号中的每个值)转换为 python 列表形式?
- actions-on-google - AndroidTV 上的 Google 助理不显示表格
- angular - (Angular 2/4/5/6)更改浏览器后退按钮的导航
- sql-server - SQL Server:从调用其他存储过程的存储过程中捕获临时表
- python - 在 DjangoModelFactory 中使用 Django faker 创建的克隆模型字段
- javascript - TypeScript 针对错误的 ECMAScript 版本
- firebase-realtime-database - firebase 功能 INVALID_ARGUMENT HTTP 错误 400 在部署
- javascript - 在输入 4 位密码时删除最后一个字母的字母间距?
- r - 如何根据第一个数据帧中的日期顺序合并数据帧,从第二个数据帧中选择一个字段
- javascript - Serial API 中的 navigator.serial 和 SerialPort 上的函数是否未实现?