是std :: list線程安全嗎?我假設它不是,所以我添加了我自己的同步機制(我認爲我有合適的期限)。但我仍然遇到問題std :: list線程push_back,前面,pop_front
每個函數都由一個單獨的線程調用。線程1等不及了,它必須儘可能快地
std::list<CFoo> g_buffer;
bool g_buffer_lock;
void thread1(CFoo frame) {
g_buffer_lock = true ;
g_buffer.push_back(frame) ;
g_buffer_lock = false;
}
void thread2()
{
while(g_buffer_lock) {
// Wait
}
// CMSTP_Send_Frame * pMSTPFrame = NULL ;
while (! g_buffer_lock && g_buffer.size() > 0)
{
// Get the top item
CFoo& pFoo = g_buffer.front() ;
// Do something.
// remove the front item
g_buffer.pop_front();
}
}
經過約170,000調用線程1和900K調用線程2我對CFoo& pFoo = g_buffer.front() ;
這會導致程序崩潰得到一個異常錯誤。 stdthrow.cpp:22
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
{
::_CrtDbgBreak();
}
}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
{ // report error and die
_Debug_message((wchar_t *) message, (wchar_t *) file, line);
}
#endif
建議,意見,是否有更好的做事方式?
是std :: list線程安全嗎? No. – KeatsPeeks 2009-12-03 22:48:57