Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / MFC

The Practical Guide to Multithreading - Part 2

Rate me:
Please Sign up or sign in to vote.
4.96/5 (119 votes)
12 Apr 2010CPOL43 min read 150.7K   5K   316  
More of practical situations to use multithreading!
// ParallelSearch_MFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ParallelSearch_MFC.h"
#include "ParallelSearch_MFCDlg.h"

#include "RelationalSearchPage.h"
#include "PrimeNumberSearchPage.h"
#include "DivisibilitySearchPage.h"

#include <algorithm>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif







CParallelSearchDlg::CParallelSearchDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CParallelSearchDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CParallelSearchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RANDOM_COUNT, m_cCount);
	DDX_Control(pDX, IDC_GEN_PROGRESS, m_cGenProgress);
}

BEGIN_MESSAGE_MAP(CParallelSearchDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &CParallelSearchDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CParallelSearchDlg message handlers

BOOL CParallelSearchDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// Default to 1 million / 10 lakhs
	m_cCount.SetWindowText( _T("1000000"));

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CParallelSearchDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CParallelSearchDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

struct NumberGenerator
{
	int CurrentNumber;

	int operator()()
	{
		return CurrentNumber++;
	}

};
void CParallelSearchDlg::OnBnClickedOk()
{
	m_gNumberCollection.m_nCount= GetDlgItemInt(IDC_RANDOM_COUNT);

	// Allocate first
	m_gNumberCollection.m_pNumbers = new int[m_gNumberCollection.m_nCount ];

	// Seed random, so that it rand function generates unique random on each run
	srand( static_cast<int> ( time(0) ) );


	// [STL]

	NumberGenerator num_gen;
	num_gen.CurrentNumber = 1;	// Start with 1

	std::generate(m_gNumberCollection.m_pNumbers, 
		m_gNumberCollection.m_pNumbers + m_gNumberCollection.m_nCount, 
		num_gen);		// Would call operator () to get next number


	// Now shuffle it
	std::random_shuffle(m_gNumberCollection.m_pNumbers, 		
		m_gNumberCollection.m_pNumbers + m_gNumberCollection.m_nCount);

	// [STL]


	OnOK();

	CPropertySheet SearchSheet( _T("Parallel Search"));;


	CRelationalSearchPage RelationSearchPage;
	CPrimeNumberSearchPage PrimeNumberSearchPage;
	CDivisibilitySearchPage DivisibilitySearchPage;

	SearchSheet.AddPage(&RelationSearchPage);
	SearchSheet.AddPage(&PrimeNumberSearchPage);
	SearchSheet.AddPage(&DivisibilitySearchPage);

	SearchSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;

	SearchSheet.DoModal();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

Comments and Discussions