//包含头文件
#inlclude<afxmt.h>
struct prodata
{
CThreadDlg *P ;
};
prodata m_Prodata;
CEvent g_evet;//事件
CMutex g_mutex;//互斥量
CSemaphore g_cSHE(2,2);//信号量;
CCriticalSection g_criSection; //定义临界区变量
static int nCount = 0;//公共变量
UINT Thread1( LPVOID lparam );//事件
UINT Thread2( LPVOID lparam );//事件
UINT Thread3( LPVOID lparam );//互斥量
UINT Thread4( LPVOID lparam );//互斥量
UINT Thread5( LPVOID lparam );//信号量
UINT Thread6( LPVOID lparam );//信号量
UINT Thread7( LPVOID lparam );//信号量
UINT Thread8( LPVOID lparam );//临界区
UINT Thread9( LPVOID lparam );//临界区
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
UINT Thread1( LPVOID lparam )
{
prodata * pProdata = ( prodata *)lparam;
//WaitForSingleObject( g_evet.m_hObject , 0xffffffff);
g_evet.ResetEvent();
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount++ );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT1 , strText );
Sleep(1000);
if( i%2 == 0)
g_evet.SetEvent();
}
g_evet.SetEvent();
return 0;
}
UINT Thread2( LPVOID lparam )
{
prodata * pProdata = ( prodata *)lparam;
WaitForSingleObject( g_evet.m_hObject , INFINITE);
g_evet.ResetEvent();//设为有信号状态;
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount-- );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT2 , strText );
Sleep(1000);
if(i%3==0)
g_evet.SetEvent();//设为无信号状态;
}
g_evet.SetEvent();
return 0;
}
UINT Thread3( LPVOID lparam )//互斥量
{
prodata * pProdata = ( prodata *)lparam;
g_mutex.Lock();
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount++ );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT1 , strText );
Sleep(1000);
if( i%2 == 0)
g_mutex.Unlock();// 让Thread4执行
}
g_mutex.Unlock();//让Thread4执行
return 0;
}
UINT Thread4( LPVOID lparam )//互斥量
{
prodata * pProdata = ( prodata *)lparam;
g_mutex.Lock();
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount-- );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT2 , strText );
Sleep(1000);
if(i%3==0)
g_mutex.Unlock();//让Thread3执行;
}
g_mutex.Unlock();
return 0;
}
UINT Thread5( LPVOID lparam )//信号量
{
prodata * pProdata = ( prodata *)lparam;
g_cSHE.Lock();
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount++ );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT1 , strText );
Sleep(1000);
if( i%2 == 0)
g_cSHE.Unlock();// 让Thread4执行
}
g_cSHE.Unlock();//让Thread4执行
return 0;
}
UINT Thread6( LPVOID lparam )//信号量
{
prodata * pProdata = ( prodata *)lparam;
g_cSHE.Lock();
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount-- );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT2 , strText );
Sleep(1000);
if(i%3==0)
g_cSHE.Unlock();//让Thread3执行;
}
g_cSHE.Unlock();
return 0;
}
UINT Thread7( LPVOID lparam )//信号量
{
prodata * pProdata = ( prodata *)lparam;
g_cSHE.Lock();
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount++ );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT3 , strText );
Sleep(1000);
if(i%3==0)
g_cSHE.Unlock();//让Thread3执行;
}
g_cSHE.Unlock();
return 0;
}
UINT Thread8( LPVOID lparam )//临界区
{
prodata * pProdata = ( prodata *)lparam;
g_criSection.Lock( );//给所有使用的资源上锁;
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount++ );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT2 , strText );
Sleep(1000);
if(i%3==0)
g_criSection.Unlock();//让Thread3执行;
}
//g_criSection.Unlock();
g_criSection.Unlock();
return 0;
}
UINT Thread9( LPVOID lparam )//临界区
{
prodata * pProdata = ( prodata *)lparam;
g_criSection.Lock( );
for ( int i = 0 ; i < 20 ; i ++ )
{
CString strText;
strText.Format("%d",nCount++ );
SetDlgItemText( pProdata->P->GetSafeHwnd() , IDC_EDIT3 , strText );
Sleep(1000);
if(i%3==0)
g_criSection.Unlock();//让Thread3执行;
}
g_criSection.Unlock();
return 0;
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CThreadDlg 对话框
CThreadDlg::CThreadDlg(CWnd* pParent /*=NULL*/)
: CDialog(CThreadDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CThreadDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CThreadDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_BN_CLICKED(IDOK2, OnBnClickedOk2)
ON_BN_CLICKED(IDOK3, OnBnClickedOk3)
ON_BN_CLICKED(IDOK4, OnBnClickedOk4)
END_MESSAGE_MAP()
// CThreadDlg 消息处理程序
BOOL CThreadDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
void CThreadDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CThreadDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CThreadDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CThreadDlg::OnBnClickedOk()//事件
{
m_Prodata.P = this;
AfxBeginThread( Thread1 , &m_Prodata);
AfxBeginThread( Thread2 , &m_Prodata);
}
void CThreadDlg::OnBnClickedOk2()
{
m_Prodata.P = this;
AfxBeginThread( Thread3 , &m_Prodata);
AfxBeginThread( Thread4 , &m_Prodata);
}
void CThreadDlg::OnBnClickedOk3()//信号量
{
m_Prodata.P = this;
AfxBeginThread( Thread5 , &m_Prodata);
AfxBeginThread( Thread6 , &m_Prodata);
AfxBeginThread( Thread7 , &m_Prodata);
}
void CThreadDlg::OnBnClickedOk4()
{
m_Prodata.P = this;
AfxBeginThread( Thread8 , &m_Prodata);
AfxBeginThread( Thread9 , &m_Prodata);
}