STDMETHODIMP CMyFilter::Stop()
{
HRESULT hr;
CAutoLock lock_it(m_pLock);
hr = CBaseFilter::Stop(); // this calls Inactivate() which locks
the "streaming lock"
// Safe to destroy filter resources used by the streaming thread.
// do stuff that affects filter state happens here
return hr;
}
HRESULT CMyPin::Receive(IMediaSample *pIMediaSample)
{
CAutoLock lock(&m_StreamingLock); // "streaming lock"
HRESULT hr;
// Check all is well with the base class, if not, drop it.
hr = CBaseInputPin::Receive(pIMediaSample);
if(hr != S_OK)
{
return hr;
}
CAutoLock AutoLock(m_pLock); // "filter lock"
// do stuff that affects filter state happens here
return S_OK;
}
> I've got a renderer filter which saves information to
> disk. I'm getting a deadlock between CMyFilter::Stop and
> CMyPin::Receive. Is it alright to change the filter
> state from inside Receive? I've seen other filters doing
> the same, but they have no deadlocks.
No, it's not. The filter state *must* reflect the graph
state and should only be changed by the graph manager. When
trying to change the state directly from the streaming
thread, a deadlock is commonly expected.
What are you trying to accomplish?
--
// Alessandro Angeli
// MVP :: DirectShow / MediaFoundation
// mvpnews at riseoftheants dot com
// http://www.riseoftheants.com/mmx/faq.htm
I have some data which needs to be read and written from both the
streaming thread and application thread, so I was trying to set the
application thread lock to protect that data.
> I have some data which needs to be read and written from
> both the streaming thread and application thread, so I
> was trying to set the application thread lock to protect
> that data.
I don't think I am understanding you. What has thread
synchronization got to do with stopping the filter? To
synchronize you just need to block waiting for the
synchronization objects (and, if you have multiple objects,
you must be careful to always wait on them in the correct
order to avoid deadlocks).
If you can provide more details on what you want to do and
how, we might be able to help you.