我的代码在调试模式下运行良好,但在发布模式下失败。
下面是我的代码片段,它失败了:
LOADER->AllocBundle(&m_InitialContent);
while(!m_InitialContent.isReady())
{
this->LoadingScreen();
}AllocBundle()将加载m_InitialContent中包含的内容,并在完成时将其就绪状态设置为true。这是使用多线程实现的。
this->LoadingScreen()应该呈现一个加载屏幕,但是目前还没有实现,所以函数有一个空体。
显然,这可能是错误的原因:如果我给函数LoadingScreen()一行代码:std::cout<<"Loading"<<std::endl;,那么它将运行良好。
如果我没有,那么代码就会卡在while(!m_InitialContent.isReady()) --它甚至不会跳到括号(this->LoadingScreen();)之间的代码。显然,它也没有更新while语句中的表达式,因为它永远停留在那里。
有人知道是什么导致了这一切吗?如果是的话,问题可能是什么?我完全迷惑不解。
编辑:根据要求追加代码
ContentLoader成员:details::ContentBundleAllocator m_CBA;
void ContentLoader::AllocBundle(ContentBundle* pBundle)
{
ASSERT(!(m_CBA.isRunning()), "ContentBundleAllocator is still busy");
m_CBA.Alloc(pBundle, m_SystemInfo.dwNumberOfProcessors);
}
void details::ContentBundleAllocator::Alloc(ContentBundle* pCB, UINT numThreads)
{
m_bIsRunning = true;
m_pCB = pCB;
pCB->m_bIsReady = false;
m_NumRunningThrds = numThreads;
std::pair<UINT,HANDLE> p;
for (UINT i = 0; i < numThreads; ++i)
{
p.second = (HANDLE)_beginthreadex(NULL,
NULL,
&details::ContentBundleAllocator::AllocBundle,
this,
NULL,&p.first);
SetThreadPriority(p.second,THREAD_PRIORITY_HIGHEST);
m_Threads.Insert(p);
}
}
unsigned int __stdcall details::ContentBundleAllocator::AllocBundle(void* param)
{
//PREPARE
ContentBundleAllocator* pCBA = (ContentBundleAllocator*)param;
//LOAD STUFF [collapsed for visibility+]
//EXIT===========================================================================================================
pCBA->m_NumRunningThrds -= 1;
if (pCBA->m_NumRunningThrds == 0)
{
pCBA->m_bIsRunning = false;
pCBA->m_pCB->m_bIsReady = true;
pCBA->Clear();
#ifdef DEBUG
std::tcout << std::endl;
#endif
std::tcout<<_T("exiting allocation...")<<std::endl;
}
std::tcout<<_T("exiting thread...")<<std::endl;
return 0;
}
bool isReady() const {return m_bIsReady;}发布于 2012-05-10 14:44:48
当您在Debug模式下编译代码时,编译器在幕后做了很多事情,防止程序员犯了许多错误,从而使应用程序崩溃。当你在释放中运行时,所有的赌注都取消了。如果您的代码不正确,那么您在发布时比在Debug中更有可能崩溃。
有几件事要检查:
确保所有变量都是正确的,intialized
catch异常,您没有预料到,然后继续运行,就好像什么都没有发生一样。发布于 2012-05-10 15:13:15
您正在从不同的线程访问变量m_bIsReady,没有内存障碍。这是错误的,因为它可能被优化器或处理器缓存缓存。您必须保护此变量免受CriticalSection、互斥或库中任何可用的同步原语的同时访问。
请注意,可能会有更多的错误,但这次肯定也是一个错误。经验法则是:从不同线程访问的每个变量都必须使用互斥/关键部分/任何东西来保护。
发布于 2012-05-10 15:00:21
快速地看,m_NumRunningThrds似乎不受同时访问的保护,因此if (pCBA->m_NumRunningThrds == 0)可能永远不会满足。
https://stackoverflow.com/questions/10536309
复制相似问题