Creating a new document in a MDI application

Suppose you want to create a new document manually in your MDI application. The following code allows to do this. It assumes that theApp is a CWinApp derived class holding a pointer m_pMyDocTpl to the CMultiDocTemplate which will instantiate your document. But you can use the CWinApp::GetFirstDocTemplatePosition() and CWinApp::GetNextDocTemplate() functions to select the type of document template.

CMultiDocTemplate* pDocTpl = theApp.m_pDocTpl;
CDocument* pDoc     = pMyDocTpl->CreateNewDocument();
CFrameWnd* pFrame   = pDocTpl->CreateNewFrame( pDoc, NULL);
pDocTpl->InitialUpdateFrame( pFrame, pDoc);

Getting list control events for a CListView

Imagine you have a CView derived view, like a CListView. You would like to do some treatments when events occurs on the list control ,like a double click. Since you don’t know the control ID of the CListCtrl in the CListView, you can’t just do a :

ON_NOTIFY(NM_DBLCLK, IDC_VIEWLISTCTRL, OnDblclkList)

This is the utilitiy of ON_NOTIFY_REFLECT

ON_NOTIFY(NM_DBLCLK, OnDblclkList)

Try it, it’s magic, it works !

Concurrent access to a small variable

When multithread-programming, you obviously need some synchronization mechanisms, such as semaphores, critical sections. This avoids that two threads access a resource at the same time, one for writing, the other one for reading.

Until now, I was cautiously protecting any variable concurrently accessed with those mechanisms.

And it took me a while to realize that it is not always necessary. As long as your variable is small enough to be read or set in one row from memory, you don’t need to protect it because a thread modifying this variable cannot be interrupted during the modification. The size of variables depends on your type of processor : must not exceed 32 bits on a 32 bits processo, 64 on a 64 bits processor.