首页 > 解决方案 > 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中。什么会导致这个问题?

标签: c++multithreading

解决方案


推荐阅读