滴水三期 (win32作业_抢红包源码)临界区/互斥体

效果图:

// Grab_red_envelope.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <windows.h>

HWND Hwnd_ExeHwnd;
HANDLE hHandleArr[3];

DWORD money_total=0;
DWORD money_1=0;
DWORD money_2=0;
DWORD money_3=0;

//CRITICAL_SECTION cs;  //临界区

HANDLE g_mutex;    //互斥体
TCHAR szbuffer1[10];
TCHAR szbuffer2[10];

void init_edit()
{
    SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),"0");
    SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_1),"0");
    SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_2),"0");
    SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_3),"0");    
}

DWORD  WINAPI edit1_pro(LPVOID lpParameter)
{
    while(TRUE)
    {
        //EnterCriticalSection(&cs);
        WaitForSingleObject(g_mutex,-1);
        if (money_total==0)
        {
            //LeaveCriticalSection(&cs);
            ReleaseMutex(g_mutex);
            break;
        }
        if (money_total<50)
        {
            money_1 +=money_total;
            money_total=0;
        }else
        {
            money_total -=50;
            money_1      +=50;
        
        }    
        memset(szbuffer1,0,10);
        memset(szbuffer2,0,10);
        sprintf(szbuffer1,"%d",money_1);
        sprintf(szbuffer2,"%d",money_total);

        SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_1),szbuffer1);
        SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer2);
    //    LeaveCriticalSection(&cs);
        ReleaseMutex(g_mutex);
        Sleep(30);
    }
    return 0;
}

DWORD  WINAPI edit2_pro(LPVOID lpParameter)
{
    while(TRUE)
    {
        //EnterCriticalSection(&cs);
        WaitForSingleObject(g_mutex,-1);
        if (money_total==0)
        {
        //    LeaveCriticalSection(&cs);
            ReleaseMutex(g_mutex);
            break;
        }
        if (money_total<50)
        {
            money_2 +=money_total;
            money_total=0;
        }else
        {
            money_total -=50;
            money_2      +=50;    
        }
            //此处进行操作编辑框.
        memset(szbuffer1,0,10);
        memset(szbuffer2,0,10);
        sprintf(szbuffer1,"%d",money_2);
        sprintf(szbuffer2,"%d",money_total);

        SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_2),szbuffer1);
        SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer2);

        //LeaveCriticalSection(&cs);
        ReleaseMutex(g_mutex);
        Sleep(30);
    }
    return 0;
}

DWORD  WINAPI edit3_pro(LPVOID lpParameter)
{
    while(TRUE)
    {
        //EnterCriticalSection(&cs);
            WaitForSingleObject(g_mutex,-1);
        if (money_total<=0)
        {
        //    LeaveCriticalSection(&cs);
            ReleaseMutex(g_mutex);
            break;
        }
        if (money_total<50)
        {
            money_3 +=money_total; //本人犯错误,把这里写成money1了。
            money_total=0;
        }else
        {
                money_total -=50;
                money_3      +=50;
        }
            //此处进行操作编辑框.
        memset(szbuffer1,0,10);
        memset(szbuffer2,0,10);
        sprintf(szbuffer1,"%d",money_3);
        sprintf(szbuffer2,"%d",money_total);
        SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_3),szbuffer1);
        SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer2);
        //LeaveCriticalSection(&cs);
        ReleaseMutex(g_mutex);
        Sleep(30);
    }
    return 0;
}

DWORD  WINAPI edit_main_pro(LPVOID lpParameter)
{
    //InitializeCriticalSection(&cs);
    g_mutex=CreateMutex(NULL, FALSE, "XYZ");
    memset(szbuffer1,0,10);
    GetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer1,10);
    sscanf(szbuffer1,"%d",&money_total);//
    
    if (money_total <=0)
    {
        MessageBox(0,"红包总数不能为0",0,0);
        return 0;
}

    hHandleArr[0]=CreateThread(NULL,0,edit1_pro,NULL,0,NULL);
    hHandleArr[1]=CreateThread(NULL,0,edit2_pro,NULL,0,NULL);
    hHandleArr[2]=CreateThread(NULL,0,edit3_pro,NULL,0,NULL);
    WaitForMultipleObjects(3,hHandleArr,FALSE,-1);//等待3个线程,只要有一个结束,就消息已通知。-1代表永远等待。
    CloseHandle(hHandleArr[0]);
    CloseHandle(hHandleArr[1]);
    CloseHandle(hHandleArr[2]);
    return 0;
}

BOOL CALLBACK MAIN_PROC(                                    
                             HWND hwndDlg,  // handle to dialog box            
                             UINT uMsg,     // message            
                             WPARAM wParam, // first message parameter            
                             LPARAM lParam  // second message parameter            
                             )    
{

Hwnd_ExeHwnd=hwndDlg;
    switch(uMsg)                                
    {
    case    WM_CLOSE:
        EndDialog(hwndDlg,0);
        break; 
    case  WM_INITDIALOG :    
        init_edit();
        break;         
    case  WM_COMMAND :    
        switch(LOWORD(wParam))
        {
        case IDC_BUTTON_START:
            HANDLE handle_main=CreateThread(NULL,0,edit_main_pro,NULL,0,NULL);
            CloseHandle(handle_main);
            break;     
        }
            break;     
    }
    return FALSE ;    
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    DialogBox(hInstance,MAKEINTRESOURCE (IDD_DIALOG_MAIN),NULL,MAIN_PROC);
    return 0;
}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值