📄 pcdlg.cpp
字号:
// PCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PC.h"
#include "PCDlg.h"
#include "afxmt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//CSemaphore semaphoreWrite(2,2); //资源最多访问线程2个,当前可访问线程数2个
char g_Array[10]={0,0,0,0,0,0,0,0,0};
HANDLE g_hMutex=CreateMutex(NULL,FALSE,NULL); //用于线程间的互斥
HANDLE g_hFullSemaphore=CreateSemaphore(NULL,10,10,NULL); //当缓冲区满时迫使生产者等待
HANDLE g_hEmptySemaphore=CreateSemaphore(NULL,10,10,NULL); //当缓冲区空时迫使消费者等待
int in=0,proempty=10,out=0,confull=0;
CString ss;
struct pro1
{
CEdit *pEdit1;//指向m_ProNum
CEdit *pEdit2;// 指向m_sin
CEdit *pEdit3;//指向m_empty
CEdit *pEdit4;//指向m_producer
CEdit *pEdit5;//指向m_number
CEdit *pEdit6;//指向m_full
}pro;
struct con
{
CEdit *pEdit1;//指向m_ConNum
CEdit *pEdit2;// 指向m_out
CEdit *pEdit3;//指向m_full
CEdit *pEdit4;//指向m_consumernum
CEdit *pEdit5;//指向m_m_consumnum
CEdit *pEdit6;//指向m_m_empty
CEdit *pEdit7;//指向m_ProNum
}consu;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPCDlg dialog
CPCDlg::CPCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPCDlg)
// 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);
}
void CPCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPCDlg)
DDX_Control(pDX, IDC_CONSUM2, m_consumnum);
DDX_Control(pDX, IDC_FULL, m_full);
DDX_Control(pDX, IDC_SOUT, m_out);
DDX_Control(pDX, IDC_COSUMER, m_consumernum);
DDX_Control(pDX, IDC_CONNUM, m_ConNum);
DDX_Control(pDX, IDC_NUMBER1, m_number);
DDX_Control(pDX, IDC_PRODU, m_producer);
DDX_Control(pDX, IDC_EMPTY, m_empty);
DDX_Control(pDX, IDC_SIN, m_sin);
DDX_Control(pDX, IDC_PRONUM, m_ProNum);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPCDlg, CDialog)
//{{AFX_MSG_MAP(CPCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PRODUCE, OnProduce)
ON_BN_CLICKED(IDC_CONSUME, OnConsume)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPCDlg message handlers
BOOL CPCDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CPCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
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 CPCDlg::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 CPCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*void Producer(CEdit *pEdit)
{
int i;
CString str;
for(i=0;i<10;i++)
{
pEdit->GetWindowText(str);
g_Array[i]='A';
str=str+g_Array[i];
pEdit->SetWindowText(str);
Sleep(1000);
}*/
//生产者
UINT WriteA(LPVOID pParam)
{
CPCDlg dlg;
CEdit *pEdit=(CEdit*)(pro.pEdit1);
pEdit->SetWindowText("");
//WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
while(1)
{
WaitForSingleObject(g_hFullSemaphore,INFINITE);
WaitForSingleObject(g_hMutex,INFINITE);
g_Array[in]='A';
//输出in
in++;
in=in%10;
CEdit *pSin=(CEdit*)(pro.pEdit2);
CString str2;
str2.Format("%d",in);
pSin->SetWindowText(str2);
proempty--;
confull++;
if(confull>10)
confull=10;
//输出缓冲区的数据
pEdit->SetWindowText( g_Array);
//输出producer
CEdit *pProducer=(CEdit*)(pro.pEdit4);
CString str1;
str1.Format("%c",'A');
pProducer->SetWindowText(str1);
// 输出number
CEdit *pnumber=(CEdit*)(pro.pEdit5);
pnumber->SetWindowText(str1);
//输出empty
CEdit *pEmpty=(CEdit*)(pro.pEdit3);
CString str3;
if(proempty<0)
proempty=0;
str3.Format("%d",proempty);
pEmpty->SetWindowText(str3);
//输出full
CEdit *pFull=(CEdit*)(pro.pEdit6);
CString str5;
str5.Format("%d", confull);
pFull->SetWindowText(str5);
Sleep(1500);
ReleaseMutex(g_hMutex);
ReleaseSemaphore(g_hEmptySemaphore,1,NULL);
}
//ReleaseSemaphore(g_hFullSemaphore,1,NULL);
//return(0);
}
UINT WriteB(LPVOID pParam)
{
CEdit *pEdit=(CEdit*)(pro.pEdit1);
pEdit->SetWindowText("");
//WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
while(1)
{
WaitForSingleObject(g_hFullSemaphore,INFINITE);
WaitForSingleObject(g_hMutex,INFINITE);
g_Array[in]='B';
//输出in
in++;
in=in%10;
CEdit *pSin=(CEdit*)(pro.pEdit2);
CString str2;
str2.Format("%d",in);
pSin->SetWindowText(str2);
proempty--;
confull++;
if(confull>10)
confull=10;
//输出缓冲区的数据
pEdit->SetWindowText( g_Array);
//输出producer
CEdit *pProducer=(CEdit*)(pro.pEdit4);
CString str1;
str1.Format("%c",'B');
pProducer->SetWindowText(str1);
// 输出number
CEdit *pnumber=(CEdit*)(pro.pEdit5);
pnumber->SetWindowText(str1);
//输出empty
CEdit *pEmpty=(CEdit*)(pro.pEdit3);
CString str3;
if(proempty<0)
proempty=0;
str3.Format("%d",proempty);
pEmpty->SetWindowText(str3);
//输出full
CEdit *pFull=(CEdit*)(pro.pEdit6);
CString str5;
str5.Format("%d", confull);
pFull->SetWindowText(str5);
Sleep(1500);
ReleaseMutex(g_hMutex);
ReleaseSemaphore(g_hEmptySemaphore,1,NULL);
}
//ReleaseSemaphore(g_hFullSemaphore,1,NULL);
//return(0);
}
UINT WriteC(LPVOID pParam)
{
CPCDlg dlg;
CEdit *pEdit=(CEdit*)(pro.pEdit1);
pEdit->SetWindowText("");
//WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
while(1)
{
WaitForSingleObject(g_hFullSemaphore,INFINITE);
WaitForSingleObject(g_hMutex,INFINITE);
g_Array[in]='C';
//输出in
in++;
in=in%10;
CEdit *pSin=(CEdit*)(pro.pEdit2);
CString str2;
str2.Format("%d",in);
pSin->SetWindowText(str2);
proempty--;
confull++;
if(confull>10)
confull=10;
//输出缓冲区的数据
pEdit->SetWindowText( g_Array);
//输出producer
CEdit *pProducer=(CEdit*)(pro.pEdit4);
CString str1;
str1.Format("%c",'C');
pProducer->SetWindowText(str1);
// 输出number
CEdit *pnumber=(CEdit*)(pro.pEdit5);
pnumber->SetWindowText(str1);
//输出empty
CEdit *pEmpty=(CEdit*)(pro.pEdit3);
CString str3;
if(proempty<0)
proempty=0;
str3.Format("%d",proempty);
pEmpty->SetWindowText(str3);
//输出full
CEdit *pFull=(CEdit*)(pro.pEdit6);
CString str5;
str5.Format("%d", confull);
pFull->SetWindowText(str5);
Sleep(1500);
ReleaseMutex(g_hMutex);
ReleaseSemaphore(g_hEmptySemaphore,1,NULL);
}
//ReleaseSemaphore(g_hFullSemaphore,1,NULL);
//return(0);
}
UINT WriteD(LPVOID pParam)
{
CPCDlg dlg;
CEdit *pEdit=(CEdit*)(pro.pEdit1);
pEdit->SetWindowText("");
//WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
while(1)
{
WaitForSingleObject(g_hFullSemaphore,INFINITE);
WaitForSingleObject(g_hMutex,INFINITE);
g_Array[in]='D';
//输出in
in++;
in=in%10;
CEdit *pSin=(CEdit*)(pro.pEdit2);
CString str2;
str2.Format("%d",in);
pSin->SetWindowText(str2);
proempty--;
confull++;
if(confull>10)
confull=10;
//输出缓冲区的数据
pEdit->SetWindowText( g_Array);
//输出producer
CEdit *pProducer=(CEdit*)(pro.pEdit4);
CString str1;
str1.Format("%c",'D');
pProducer->SetWindowText(str1);
// 输出number
CEdit *pnumber=(CEdit*)(pro.pEdit5);
pnumber->SetWindowText(str1);
//输出empty
CEdit *pEmpty=(CEdit*)(pro.pEdit3);
CString str3;
if(proempty<0)
proempty=0;
str3.Format("%d",proempty);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -