Solved

About CCriticalSection

Posted on 1998-11-18
2
197 Views
Last Modified: 2013-11-20
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_readSection, FALSE);
m_readSection.Lock(INFINITE);
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_pDoc->m_readSect);
{
      m_pDoc->ReadMsg(pMsg, this);
}
LeaveCriticalSection(&m_pDoc->m_readSect);

it's OK.

What's wrong with the use of MFC classes?





0
Comment
Question by:visualc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 50 total points
ID: 1324929
The problem is you are passing the critical section by value. Generally this makes a copy and most MFC classes do not have copy semantics. You should pass the critical section by reference (CCriticalSection&) or by pointer (CCriticalSection*).
0
 

Author Comment

by:visualc
ID: 1324930
Hi,
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_readSection, FALSE);
if (m_readSection.IsLocked())
    MessageBeeP(1000);
m_readSection.Lock(INFINITE);
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
withoutTen challenge 14 142
Sed question 2 139
ERP and the other system process verification 5 26
How do I get Window Title of all opened process? 4 61
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question