Re: STL vector and multithreading
John <John@discussions.microsoft.com> wrote:
DWORD dwWaitResult = WaitForSingleObject(m_hMutex, 5000L);
switch (dwWaitResult) {
case WAIT_TIMEOUT:
case WAIT_ABANDONED:
default:
hr = E_FAIL;
Note that WAIT_ABANDONED means that you did in fact acquired the mutex.
In some sense, it's a success code, not a failure code. Of course, this
code usually means that some other thread has encountered a bug.
And finally here is the code that runs in the worker thread that
iterates and does
the callbacks
HRESULT hr = S_OK;
DWORD dwWaitResult = WaitForSingleObject(h_hMutex, 5000L);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
{
vector < CCallBackItem *>::iterator iter;
for (iter = m_vecAdvise.begin(); iter < m_vecAdvise.end(); iter++)
(*iter)->m_pCallbackFunc->CallbackFunc();
}
break;
What does CallbackFunc do? Can it call Advise or SetPriority? If it
does, it would interfere with your iteration, since it would rearrange
the vector from under you.
Also note that you are calling CallbackFunc while holding a mutex. If it
takes a long time to return, it will block access to the vector for all
other threads.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925