Click here to Skip to main content
15,905,563 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CMainFrame::PreCreateWindow(CREATESTRUCT& cs) Pin
Fazlul Kabir26-Oct-01 7:03
Fazlul Kabir26-Oct-01 7:03 
GeneralRe: CMainFrame::PreCreateWindow(CREATESTRUCT& cs) Pin
Fazlul Kabir26-Oct-01 7:40
Fazlul Kabir26-Oct-01 7:40 
GeneralRe: CMainFrame::PreCreateWindow(CREATESTRUCT& cs) Pin
vhalik26-Oct-01 7:50
vhalik26-Oct-01 7:50 
GeneralCritical Section problems... Pin
LukeV26-Oct-01 5:59
LukeV26-Oct-01 5:59 
GeneralRe: Critical Section problems... Pin
Joaquín M López Muñoz26-Oct-01 7:16
Joaquín M López Muñoz26-Oct-01 7:16 
GeneralRe: Critical Section problems... Pin
LukeV26-Oct-01 7:49
LukeV26-Oct-01 7:49 
GeneralRe: Critical Section problems... Pin
Joaquín M López Muñoz26-Oct-01 8:30
Joaquín M López Muñoz26-Oct-01 8:30 
GeneralRe: Critical Section problems... Pin
Tim Deveaux26-Oct-01 10:20
Tim Deveaux26-Oct-01 10:20 
This doesn't really answer your question, but here's the basics of a little class that can be used for simple locks:

CX_Lock::CX_Lock(CRITICAL_SECTION *cs){
    EnterCriticalSection(cs);
    m_pCriticalSection = cs;        // ok - we're in - save for exit.
}
 
CX_Lock::~CX_Lock(){
    LeaveCriticalSection(m_pCriticalSection);   // all we need to do.
}


Now, for a class whose, say, accessor functions should be guarded, declare a member CS:

CRITICAL_SECTION    m_cs;               // synchronization object


And don't forget to call InitializeCriticalSection(&m_cs); in the constructor and DeleteCriticalSection(&m_cs); in the destructor.

Then inside a block of code in a memeber fn you just declare a local lock object like so:

DWORD MyClass::GetMyMemberVar()
{
    CX_Lock lock(&m_cs);   // protect access

    return m_dwMyMemberVar;
}


And voila, you've serialized thread access to the code in the block where the lock object has scope.

This has the advantages of exception safety (unwinding the stack will call the lock's dtor and exit the CS) and less code to muck with.

As an additional nice touch, declare the default ctor private (so people will use the class properly).

Not sure where I stumbled on this - I thought the MFC had an equivalent, but not sure.
GeneralRe: Critical Section problems... Pin
HintiFlo23-Jan-02 1:12
HintiFlo23-Jan-02 1:12 
QuestionHow to set a bitmap as a dialog background ? Pin
adara26-Oct-01 4:58
adara26-Oct-01 4:58 
AnswerRe: How to set a bitmap as a dialog background ? Pin
Joaquín M López Muñoz26-Oct-01 5:48
Joaquín M López Muñoz26-Oct-01 5:48 
Generalthanx Pin
adara26-Oct-01 12:19
adara26-Oct-01 12:19 
Generaloverload resolution in VC 6.0 Pin
Patrick D Owens26-Oct-01 4:46
Patrick D Owens26-Oct-01 4:46 
GeneralRe: overload resolution in VC 6.0 Pin
Chris Losinger26-Oct-01 4:55
professionalChris Losinger26-Oct-01 4:55 
GeneralRe: overload resolution in VC 6.0 Pin
Patrick D Owens26-Oct-01 5:16
Patrick D Owens26-Oct-01 5:16 
GeneralRe: overload resolution in VC 6.0 Pin
Joaquín M López Muñoz26-Oct-01 6:53
Joaquín M López Muñoz26-Oct-01 6:53 
GeneralRe: overload resolution in VC 6.0 Pin
Patrick D Owens26-Oct-01 9:16
Patrick D Owens26-Oct-01 9:16 
GeneralFilling CListCtrl from dll problem... Pin
26-Oct-01 4:39
suss26-Oct-01 4:39 
GeneralRe: Filling CListCtrl from dll problem... Pin
Joaquín M López Muñoz26-Oct-01 7:27
Joaquín M López Muñoz26-Oct-01 7:27 
GeneralAligning text Pin
Andrew Stampor26-Oct-01 3:57
Andrew Stampor26-Oct-01 3:57 
General2-d Graphics Pin
Chambers26-Oct-01 3:52
Chambers26-Oct-01 3:52 
GeneralRe: 2-d Graphics Pin
Remi Morin26-Oct-01 8:33
Remi Morin26-Oct-01 8:33 
GeneralRe: 2-d Graphics Pin
Christian Graus26-Oct-01 11:04
protectorChristian Graus26-Oct-01 11:04 
GeneralRe: 2-d Graphics Pin
Chambers27-Oct-01 0:07
Chambers27-Oct-01 0:07 
GeneralRe: 2-d Graphics Pin
Christian Graus27-Oct-01 0:32
protectorChristian Graus27-Oct-01 0:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.