码迷,mamicode.com
首页 > 编程语言 > 详细

c++线程同步之信号量

时间:2019-07-04 21:25:58      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:上下   char   multiple   icon   ati   escape   对话框   mem   set   

技术图片

// MutexExDlg.h : 头文件
//

#pragma once


// CMutexExDlg 对话框
class CMutexExDlg : public CDialogEx
{
// 构造
public:
    CMutexExDlg(CWnd* pParent = NULL);    // 标准构造函数

// 对话框数据
    enum { IDD = IDD_MUTEXEX_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
    static HANDLE hThread[3];
    static HANDLE m_Semaphore;
    static DWORD WINAPI ThreadProc0(LPVOID lpParameter);
    static DWORD WINAPI ThreadProc1(LPVOID lpParameter);
    static DWORD WINAPI ThreadProc2(LPVOID lpParameter);
    static DWORD WINAPI ThreadProc3(LPVOID lpParameter);


// 实现
protected:
    HICON m_hIcon;

    // 生成的消息映射函数
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    afx_msg void OnBnClickedButton1();
    static int m_Edit0;
    static int m_Edit1;
    static int m_Edit2;
    static int m_Edit3;
};
// MutexExDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MutexEx.h"
#include "MutexExDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMutexExDlg 对话框



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

void CMutexExDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_Edit0);
    DDV_MinMaxInt(pDX, m_Edit0, 0, 1000);
    DDX_Text(pDX, IDC_EDIT2, m_Edit1);
    DDV_MinMaxInt(pDX, m_Edit1, 0, 1000);
    DDX_Text(pDX, IDC_EDIT3, m_Edit2);
    DDV_MinMaxInt(pDX, m_Edit2, 0, 1000);
    DDX_Text(pDX, IDC_EDIT4, m_Edit3);
    DDV_MinMaxInt(pDX, m_Edit3, 0, 1000);
}

BEGIN_MESSAGE_MAP(CMutexExDlg, CDialogEx)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON1, &CMutexExDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CMutexExDlg 消息处理程序

BOOL CMutexExDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标

    // TODO:  在此添加额外的初始化代码

    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CMutexExDlg::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
    {
        CDialogEx::OnPaint();
    }
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMutexExDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


// 重写虚函数 PreTranslateMessage 屏蔽掉Esc键和Enter键
BOOL CMutexExDlg::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        int keyCode = (int)pMsg->wParam;
        if (keyCode == VK_ESCAPE || keyCode == VK_RETURN)
        {
            return TRUE;
        }
    }
    return CDialogEx::PreTranslateMessage(pMsg);
}

HANDLE CMutexExDlg::m_Semaphore = NULL;
int CMutexExDlg::m_Edit0 = 0;
int CMutexExDlg::m_Edit1 = 0;
int CMutexExDlg::m_Edit2 = 0;
int CMutexExDlg::m_Edit3 = 0;
HANDLE CMutexExDlg::hThread[3] = { NULL };

// 抢红包按钮点击事件处理函数
void CMutexExDlg::OnBnClickedButton1()
{
    // 获取编辑框内容到str变量
    CString str;
    GetDlgItem(IDC_EDIT1)->GetWindowText(str);
    // CString 转 int
    m_Edit0 = _ttoi(str);
    m_Edit1 = 0;
    m_Edit2 = 0;
    m_Edit3 = 0;
    // 这里需要创建一个线程 因为 WaitForMultipleObjects 会阻塞住 另外把this指针作为参数传递给线程,用于子线程更新编辑框内容
    HANDLE hThread0 = ::CreateThread(NULL, NULL, ThreadProc0, this, NULL, NULL);
    CloseHandle(hThread0);
}

DWORD WINAPI CMutexExDlg::ThreadProc0(LPVOID lpParameter)
{
    CMutexExDlg* pDlg = (CMutexExDlg*)lpParameter;
    // 创建一个信号量
    m_Semaphore = ::CreateSemaphore(NULL,
        0, // 表示初始资源数量,0时不发送信号
        3, // 表示最大并发数量 lInitialCount < lMaximumCount
        NULL);
    // 创建三个线程抢红包
    hThread[0] = ::CreateThread(NULL, NULL, ThreadProc1, lpParameter, NULL, NULL);
    hThread[1] = ::CreateThread(NULL, NULL, ThreadProc2, lpParameter, NULL, NULL);
    hThread[2] = ::CreateThread(NULL, NULL, ThreadProc3, lpParameter, NULL, NULL);
    // 设置编辑框内容
    ::SetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT1), L"1000");
    // 将对象设置为已通知状态
    ReleaseSemaphore(m_Semaphore, 
        1, // 表示增加个数,必须大于0且不超过最大资源数量
        NULL);
    // 使用WaitForMultipleObjects监听所有线程,当线程全部结束后,调用CloseHandle关闭句柄.
    WaitForMultipleObjects(3, hThread, TRUE, -1);
    ::CloseHandle(hThread[0]);
    ::CloseHandle(hThread[1]);
    ::CloseHandle(hThread[2]);
    ::CloseHandle(m_Semaphore);
    return 0;
}

// 线程回调函数1
DWORD WINAPI CMutexExDlg::ThreadProc1(LPVOID lpParameter)
{
    CMutexExDlg* pDlg = (CMutexExDlg*)lpParameter;
    TCHAR szBuffer[10];
    DWORD dwTimer = 0;
    WaitForSingleObject(m_Semaphore, INFINITE);
    while (dwTimer < 100)
    {
        Sleep(50);
        memset(szBuffer, 0, 10);
        ::GetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT2), szBuffer, 10);
        swscanf(szBuffer, L"%d", &dwTimer);
        dwTimer++;
        memset(szBuffer, 0, 10);
        swprintf(szBuffer, L"%d", dwTimer);
        ::SetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT2), szBuffer);
    }
    // 递增信号量的当前资源计数        
    ReleaseSemaphore(m_Semaphore, 1, NULL);
    return 0;
}

// 线程回调函数2
DWORD WINAPI CMutexExDlg::ThreadProc2(LPVOID lpParameter)
{
    CMutexExDlg* pDlg = (CMutexExDlg*)lpParameter;
    TCHAR szBuffer[10];
    DWORD dwTimer = 0;
    WaitForSingleObject(m_Semaphore, INFINITE);
    while (dwTimer < 100)
    {
        Sleep(50);
        memset(szBuffer, 0, 10);
        ::GetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT3), szBuffer, 10);
        // 将TCHAR* 转换为 int
        swscanf(szBuffer, L"%d", &dwTimer);
        dwTimer++;
        memset(szBuffer, 0, 10);
        // int 转换为TCHAR*
        swprintf(szBuffer, L"%d", dwTimer);
        ::SetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT3), szBuffer);
    }
    // 递增信号量的当前资源计数        
    ReleaseSemaphore(m_Semaphore, 1, NULL);
    return 0;
}

// 线程回调函数3
DWORD WINAPI CMutexExDlg::ThreadProc3(LPVOID lpParameter)
{
    CMutexExDlg* pDlg = (CMutexExDlg*)lpParameter;
    TCHAR szBuffer[10];
    DWORD dwTimer = 0;
    WaitForSingleObject(m_Semaphore, INFINITE);
    while (dwTimer < 100)
    {
        Sleep(50);
        memset(szBuffer, 0, 10);
        ::GetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT4), szBuffer, 10);
        swscanf(szBuffer, L"%d", &dwTimer);
        dwTimer++;
        memset(szBuffer, 0, 10);
        swprintf(szBuffer, L"%d", dwTimer);
        ::SetWindowText(::GetDlgItem(pDlg->m_hWnd, IDC_EDIT4), szBuffer);
    }
    // 递增信号量的当前资源计数        
    ReleaseSemaphore(m_Semaphore, 1, NULL);
    return 0;
}

 

c++线程同步之信号量

标签:上下   char   multiple   icon   ati   escape   对话框   mem   set   

原文地址:https://www.cnblogs.com/duxie/p/11134923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!