c++ - c ++线程未产生导致互斥锁未解锁
问题描述
我有一个c++
-program 执行线程来处理某些项目。为了同步这些线程,我分配了池编号以防止(太多)并发线程。但是,某些线程实际上从未被线程调用的函数“接收”。为了处理线程生成,我使用以下while
循环main
:
while (true) {
for (auto& Group: Groups) {
unsigned long long ProcessingPoolNr = GetProcessingPoolNr();
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Received poolnr: " << ProcessingPoolNr << " fo group: " << FileGroup.Name << endl;
try {
thread GroupHandler(HandleGroup, ProcessingPoolNr, Group);
GroupHandler.detach();
}
catch (const std::exception &ex) {
cout << "Thread exited with exception: " << ex.what() << endl;
}
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Detached group: " << FileGroup.Name << endl;
}
SleepForSeconds(1);
}
这将执行一个线程并等待一个新的PoolNr
. (目前,只有 1 个 PoolNr 可用,因此只有 1 个并发线程在运行)。-GetProcessingPoolNr
功能:
unsigned long long GetGroupProcessingPoolNr() {
bool FoundPool = false;
unsigned long long PoolNr = 0;
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Getting poolnr" << endl;
bool Sleep = false;
while (!FoundPool) {
if (Sleep) {
SleepForMilliSeconds(1);
}
GroupProcessingPoolMutex.lock();
try {
for (auto& Pool : GroupProcessingPool) {
if (Pool.second) {
//THIS POOL IS ALREADY PROCESSING
}
else {
FoundPool = true;
PoolNr = Pool.first;
GroupProcessingPool[Pool.first] = true;
break;
}
}
}
catch (const exception& e) {
string Message =__FUNCTION__;
Message += " error: ";
Message += e.what();
Exit(Message);
}
GroupProcessingPoolMutex.unlock();
Sleep = true;
}
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Got poolnr: " << PoolNr << endl;
return PoolNr;
}
到目前为止一切顺利,但是正如您所看到的,它GroupProcessingPool
被互斥锁锁定。要释放 a PoolNr
,正在运行的线程将在最后释放它。生成的线程具有以下代码:
void HandleGroup(unsigned long long PoolNr, Group Group) {
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Handling group: " << Group.Name << endl;
//DO SOME PROCESSING HERE
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Handled group: " << Group.Name << endl;
ReleaseGroupProcessingPool(PoolNr);
}
-ReleaseGroupProcessingPool
函数是:
void ReleaseGroupProcessingPool(unsigned long long PoolNr) {
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Releasing poolnr: " << PoolNr << endl;
GroupProcessingPoolMutex.lock();
GroupProcessingPool[PoolNr] = false;
GroupProcessingPoolMutex.unlock();
cout << GetLogDateTime() << "|" << GetLogType("Info") << "|Released poolnr: " << PoolNr << endl;
}
如您所见,预期的流程类似于:
Getting PoolNr
Got PoolNr: 1
Received PoolNr: 1 for group: x
Detached group: x
Handling group: x
Handled group: x
Releasing PoolNr: 1
Released PoolNr: 1
---- 这个重复 ---
logging
因此,发生的问题function
是我缺少thread
. 似乎thread
产生了 并且detached
,但function
从未运行过。所以,mutex
永远不会被释放,因为释放发生在function
/thread
中。什么会导致这个问题?
解决方案
推荐阅读
- javascript - 为什么在 React 中使用 setState 在 componentDidUpdate 中被视为不好的做法?
- python - 如何一次为每一行以不同的方式切片张量流张量?
- python - Selenium 关闭/接受 Chrome 确认弹出窗口
- javascript - Javascript/Jquery:按顺序执行相同或不同的函数
- javascript - 无法读取未定义的属性“parentNode”(谷歌标签管理器,f.parentNode.insertBefore(j,f))
- sql-server - 将日期与特定日期格式进行比较
- c# - 通过 ICollectionView 过滤 ObservableCollection 并使用它
- docker - 无法按名称访问虚拟主机域 docker apache - 一个灯堆栈应用程序
- react-native - polidea ble编码发送包到base64
- javascript - 提供的注册令牌未注册。以前有效的注册令牌可能由于多种原因而被注销