visualc
asked on
About CCriticalSection
Hi!
I have some threads, which can call a function from a single
object, let's say CBoss.
When I declare a CCriticalSection class in the header of
CBoos ( CCriticalSection m_readSection;)
and I try to access the function of this class from a thread-class in the next way:
CSingleLock m_readSection(&m_pDoc->m_r eadSection , FALSE);
m_readSection.Lock(INFINIT E);
m_pDoc->ReadMsg(pMsg, this);
m_readSection.Unlock();
the thread stops on the second Lock (i.e. when I try to lock the second time the CriticalSection, the thread begin to wait, even if the CSingleLock object - m_readSection is
not locked- I have checked this). There is only one thread,
so doesn't exist another thread to lock this critical section.
When I use in the CBoss header
static CRITICAL_SECTION m_readSect;
and I try to access the function of this class from a thread class in the next way:
EnterCriticalSection(&m_pD oc->m_read Sect);
{
m_pDoc->ReadMsg(pMsg, this);
}
LeaveCriticalSection(&m_pD oc->m_read Sect);
it's OK.
What's wrong with the use of MFC classes?
I have some threads, which can call a function from a single
object, let's say CBoss.
When I declare a CCriticalSection class in the header of
CBoos ( CCriticalSection m_readSection;)
and I try to access the function of this class from a thread-class in the next way:
CSingleLock m_readSection(&m_pDoc->m_r
m_readSection.Lock(INFINIT
m_pDoc->ReadMsg(pMsg, this);
m_readSection.Unlock();
the thread stops on the second Lock (i.e. when I try to lock the second time the CriticalSection, the thread begin to wait, even if the CSingleLock object - m_readSection is
not locked- I have checked this). There is only one thread,
so doesn't exist another thread to lock this critical section.
When I use in the CBoss header
static CRITICAL_SECTION m_readSect;
and I try to access the function of this class from a thread class in the next way:
EnterCriticalSection(&m_pD
{
m_pDoc->ReadMsg(pMsg, this);
}
LeaveCriticalSection(&m_pD
it's OK.
What's wrong with the use of MFC classes?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think that is another problem, because if I modify the declaration of m_readSection to
CCriticalSection* m_readSection;
and I use in this way -->
CSingleLock m_readSection(m_pDoc->m_re
if (m_readSection.IsLocked())
MessageBeeP(1000);
m_readSection.Lock(INFINIT
m_pDoc->ReadMsg(pMsg, this);
m_readSection.Unlock();
the problem is the same. When I try the second time, the code doesn't reach the MessageBeep command, so the CriticalSection is unlocked, but when I try to lock, the thread stops.
I don't have any idea, why.