我的函数做的是循环遍历一个布尔数组,并且在找到一个设置为false的元素时,它被设置为true。该函数是我的内存管理器singleton类中的一个方法,它返回一个指向内存的指针。我得到一个错误,我的迭代器似乎循环并最终从头开始,我相信这是因为多个线程正在调用该函数。多线程环境中函数的错误
void* CNetworkMemoryManager::GetMemory()
{
WaitForSingleObject(hMutexCounter, INFINITE);
if(mCounter >= NetConsts::kNumMemorySlots)
{
mCounter = 0;
}
unsigned int tempCounter = mCounter;
unsigned int start = tempCounter;
while(mUsedSlots[tempCounter])
{
tempCounter++;
if(tempCounter >= NetConsts::kNumMemorySlots)
{
tempCounter = 0;
}
//looped all the way around
if(tempCounter == start)
{
assert(false);
return NULL;
}
}
//return pointer to free space and increment
mCounter = tempCounter + 1;
ReleaseMutex(hMutexCounter);
mUsedSlots[tempCounter] = true;
return mPointers[tempCounter];
}
我的错误是断言在循环中关闭。我的问题是我如何修复这个函数,并且是多线程引起的错误?
编辑:添加一个互斥锁来保护mCounter变量。不用找了。错误仍然发生。
这是什么问题? – juanchopanza 2013-02-17 20:07:58
所以基本上你问是否多线程/并发可能是此功能不能正常工作的原因? – LihO 2013-02-17 20:14:15
是的,我想知道如果这是原因,我该如何修复函数 – user998797 2013-02-17 20:17:45