Click here to Skip to main content
15,896,330 members
Articles / Desktop Programming / MFC

Using Semaphores: Multithreaded Producer/Consumer

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
14 Jun 200115 min read 321.9K   3.1K   92  
An introduction to using Semaphores
// queuetestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "queuetest.h"
#include "Queue.h"
#include "ThreadData.h"
#include "queuetestDlg.h"
#include "buzzword.h"
#include "about.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/****************************************************************************
*                              UWM_MESSAGE_MSG
* Inputs:
*       WPARAM: Thread ID of thread that sent message
*	LPARAM: (LPARAM)(CString *) String to be displayed
* Result: LRESULT
*       Logically void, 0, always
* Effect: 
*       Posted to the main GUI thread to record the item dequeued
****************************************************************************/
#define UWM_MESSAGE_MSG _T("UWM_MESSAGE-{72A92340-60F9-11d5-A02F-006067718D04}")

static UINT UWM_MESSAGE = ::RegisterWindowMessage(UWM_MESSAGE_MSG);


/****************************************************************************
*                              UWM_SHOWCOUNT
* Inputs:
*       WPARAM: Count to show
*	LPARAM: ignored
* Result: LRESULT
*       Logically void, 0, always
* Effect: 
*       Posted to the main GUI thread to record the count of items in the queue
****************************************************************************/
#define UWM_SHOWCOUNT_MSG _T("UWM_SHOWCOUNT-{72A92340-60F9-11d5-A02F-006067718D04}")

static UINT UWM_SHOWCOUNT = ::RegisterWindowMessage(UWM_SHOWCOUNT_MSG);


/////////////////////////////////////////////////////////////////////////////
// CQueuetestDlg dialog

CQueuetestDlg::CQueuetestDlg(CWnd* pParent /*=NULL*/)
	: q(20), CDialog(CQueuetestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQueuetestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	count = 0;
	seq = 0;

}

void CQueuetestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQueuetestDlg)
	DDX_Control(pDX, IDC_THREAD2_CAPTION, c_Thread2Caption);
	DDX_Control(pDX, IDC_THREAD1_CAPTION, c_Thread1Caption);
	DDX_Control(pDX, IDC_STATUS, c_Status);
	DDX_Control(pDX, IDC_QUEUESIZE, c_QueueSize);
	DDX_Control(pDX, IDC_INPUT, c_Input);
	DDX_Control(pDX, IDC_DATA2, c_Data2);
	DDX_Control(pDX, IDC_DATA1, c_Data1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQueuetestDlg, CDialog)
        ON_REGISTERED_MESSAGE(UWM_MESSAGE, OnPostedMessage)
	ON_REGISTERED_MESSAGE(UWM_SHOWCOUNT, OnShowCount)
	//{{AFX_MSG_MAP(CQueuetestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueuetestDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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

	start(c_Data1);
	start(c_Data2);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

/****************************************************************************
*                         CQueuetestDlg::OnSysCommand
* Inputs:
*       UINT nID: system command ID
*	LPARAM lParam: passed to parent if not handled here
* Result: void
*       
* Effect: 
*       Recognizes the About menu item
****************************************************************************/

void CQueuetestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	   {
	    doAbout();
	   }
	else
	   {
	    CDialog::OnSysCommand(nID, lParam);
	   }
}

// 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 CQueuetestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CQueuetestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CQueuetestDlg::OnCancel() 
{
 // body removed so ESC doesn't terminate program
}

void CQueuetestDlg::OnOK() 
{
 // body removed so <Enter> doesn't terminate program
}

void CQueuetestDlg::OnClose() 
{
 q.shutdown();
	
 CDialog::OnOK();
}

/****************************************************************************
*                            CQueuetestDlg::OnNext
* Result: void
*       
* Effect: 
*       Generates a new string and adds it to the queue
****************************************************************************/

void CQueuetestDlg::OnNext() 
{
 CString p;
 buzzphrase(p);
 CString * s = new CString;
 s->Format(_T("%03d: %s"), ++seq, p);
 c_Input.SetWindowText(*s);
 if(!q.AddTail(s))
    { /* failed */
     c_Status.SetWindowText(_T("Error: queue full"));
     return;
    } /* failed */
 else
    { /* succeeded */
     c_Status.SetWindowText(_T(""));
     long n = InterlockedIncrement(&count);
     showCount(n);
    } /* succeeded */
}

/****************************************************************************
*                          CQueuetestDlg::showCount
* Inputs:
*       long n: Count to display
* Result: void
*       
* Effect: 
*       Displays the count
****************************************************************************/

void CQueuetestDlg::showCount(long n)
    {
     PostMessage(UWM_SHOWCOUNT, n);
    } // CQueuetestDlg::showCount

/****************************************************************************
*                         CQueuetestDlg::threadStart
* Inputs:
*       LPVOID me: Reference to class
* Result: UINT
*       0, always
* Effect: 
*       Calls the main thread loop
****************************************************************************/

UINT CQueuetestDlg::threadStart(LPVOID me)
    {
     CQueuetestDlg * self = (CQueuetestDlg *)me;
     self->run();
     return 0;
    } // CQueuetestDlg::threadStart

/****************************************************************************
*                             CQueuetestDlg::run
* Result: void
*       
* Effect: 
*       Runs the worker thread
****************************************************************************/

void CQueuetestDlg::run()
    {
     CString * s = new CString;
     s->Format(_T("Thread %08x started"), ::GetCurrentThreadId());
     PostMessage(UWM_MESSAGE, (WPARAM)::GetCurrentThreadId(), (LPARAM)s);

     while(TRUE)
	{ /* thread loop */
	 LPVOID p = q.RemoveHead();
	 long n = InterlockedDecrement(&count);
	 showCount(n);
	 PostMessage(UWM_MESSAGE, (WPARAM)::GetCurrentThreadId(), (LPARAM)p);
	 ::Sleep(200 + rand() % 500); // make simulated delay
	} /* thread loop */
    } // CQueuetestDlg::run

/****************************************************************************
*                          CQueuetestDlg::OnPostedMessage
* Inputs:
*       WPARAM: Thread ID of the thread
*	LPARAM: (LPARAM)(CString *) String to add
* Result: LRESULT
*       Logically void, 0, always
* Effect: 
*       Adds the item to the list for the thread
****************************************************************************/

LRESULT CQueuetestDlg::OnPostedMessage(WPARAM wParam, LPARAM lParam)
    {
     DWORD threadID = (DWORD)wParam;
     CString * s = (CString *)lParam;
     if(c_Data1.GetThreadID() == threadID)
	 c_Data1.AddString(*s);
     else
     if(c_Data2.GetThreadID() == threadID)
	 c_Data2.AddString(*s);
     delete s;
     return 0;
    } // CQueuetestDlg::OnPostedMessage

/****************************************************************************
*                         CQueuetestDlg::OnShowCount
* Inputs:
*       WPARAM wParam: Value to display
*	LPARAM: unused
* Result: LRESULT
*       Logically void, 0, always
* Effect: 
*       Displays the count of items in the queue
****************************************************************************/

LRESULT CQueuetestDlg::OnShowCount(WPARAM wParam, LPARAM)
    {
     CString s;
     s.Format(_T("%d"), wParam);
     c_QueueSize.SetWindowText(s);
     return 0;
    } // CQueuetestDlg::OnShowCount

/****************************************************************************
*                            CQueuetestDlg::start
* Inputs:
*	CThreadDataBox & data: Data
* Result: void
*       
* Effect: 
*       Starts the thread. Associates the thread ID with the control
****************************************************************************/

void CQueuetestDlg::start(CThreadDataBox & data)
    {
     CWinThread * thread = 
			  AfxBeginThread(threadStart,             // thread function
					 this,                    // thread function parameter
					 THREAD_PRIORITY_NORMAL,  // priority
					 0,                       // use default stack size
					 CREATE_SUSPENDED);       // not running

     // The thread is created in "not running" mode so we can actually have time to
     // set up all the necessary parameters before the thread starts sending

     data.SetThreadID(thread->m_nThreadID);
     thread->ResumeThread();
    } // CQueuetestDlg::start

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.


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions