// 20171227_01.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "resource.h" #include <stdio.h> #include <windows.h> HWND hEdit_hong; HWND hEdit1; HWND hEdit2; HWND hEdit3; int HongNum = 0; HANDLE hThead_Main; HANDLE hMutex; DWORD WINAPI EditThread1( LPVOID lpParameter // thread data ) { TCHAR numStr[10]; memset(numStr,0,10); DWORD num = 0; while(HongNum>100) { HANDLE MuTex1 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"XYZ"); WaitForSingleObject(MuTex1,INFINITE); GetWindowText(hEdit1,numStr,10); sscanf(numStr,"%d",&num); Sleep(50); num+=50; HongNum-=50; //设置文本 sprintf(numStr,"%d",num); SetWindowText(hEdit1,numStr); memset(numStr,0,10); sprintf(numStr,"%d",HongNum); SetWindowText(hEdit_hong,numStr); memset(numStr,0,10); ReleaseMutex(MuTex1); } return 0; } DWORD WINAPI EditThread2( LPVOID lpParameter // thread data ) { TCHAR numStr[10]; memset(numStr,0,10); DWORD num = 0; while(HongNum>100) { HANDLE MuTex2 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"XYZ"); WaitForSingleObject(MuTex2,INFINITE); GetWindowText(hEdit2,numStr,10); sscanf(numStr,"%d",&num); Sleep(50); num+=50; HongNum-=50; //设置文本 sprintf(numStr,"%d",num); SetWindowText(hEdit2,numStr); memset(numStr,0,10); sprintf(numStr,"%d",HongNum); SetWindowText(hEdit_hong,numStr); memset(numStr,0,10); ReleaseMutex(MuTex2); } return 0; } DWORD WINAPI EditThread3( LPVOID lpParameter // thread data ) { TCHAR numStr[10]; memset(numStr,0,10); DWORD num = 0; while(HongNum>100) { HANDLE MuTex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"XYZ"); WaitForSingleObject(MuTex3,INFINITE); GetWindowText(hEdit3,numStr,10); sscanf(numStr,"%d",&num); Sleep(50); num+=50; HongNum-=50; //设置文本 sprintf(numStr,"%d",num); SetWindowText(hEdit3,numStr); memset(numStr,0,10); sprintf(numStr,"%d",HongNum); SetWindowText(hEdit_hong,numStr); memset(numStr,0,10); ReleaseMutex(MuTex3); } return 0; } DWORD WINAPI GetHongBao( LPVOID lpParameter // thread data ) { //EnterCriticalSection() //LeaveCriticalSection() //设置总的红包数 TCHAR numStr[10]; memset(numStr,0,10); GetWindowText(hEdit_hong,numStr,10); //MessageBox(0,numStr,0,0); DWORD num = 0; sscanf(numStr,"%d",&num); HongNum = num; //启动3个线程 HANDLE hThread[3]; hThread[0] = CreateThread(NULL,0,EditThread1,NULL,0,NULL); hThread[1] = CreateThread(NULL,0,EditThread2,NULL,0,NULL); hThread[2] = CreateThread(NULL,0,EditThread3,NULL,0,NULL); WaitForMultipleObjects(3,hThread,FALSE,INFINITE); //关闭所有线程 ::CloseHandle(hThread[0]); ::CloseHandle(hThread[1]); ::CloseHandle(hThread[2]); //MessageBox(0,0,0,MB_OK); return 0; } BOOL CALLBACK MainDialogProc( HWND hwndDlg, // handle to dialog box UINT uMsg, // message WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { switch(uMsg) { case WM_INITDIALOG: { //InitializeCriticalSection(&cs); hMutex = CreateMutex(NULL,FALSE,"XYZ"); hEdit_hong = GetDlgItem(hwndDlg,IDC_EDIT_HONG); SetWindowText(hEdit_hong,"0"); hEdit1 = GetDlgItem(hwndDlg,IDC_EDIT_1); hEdit2 = GetDlgItem(hwndDlg,IDC_EDIT_2); hEdit3 = GetDlgItem(hwndDlg,IDC_EDIT_3); SetWindowText(hEdit1,"0"); SetWindowText(hEdit2,"0"); SetWindowText(hEdit3,"0"); break; } case WM_CLOSE: { //DeleteCriticalSection(&cs); ::CloseHandle(hThead_Main); EndDialog(hwndDlg,0); break; } case WM_COMMAND: { switch(LOWORD(wParam)) { case IDC_BUTTON_GET: { //MessageBox(0,0,0,0); //这里开始抢红包 hThead_Main = CreateThread(NULL,0,GetHongBao,NULL,0,NULL); //WaitForSingleObject(hThead,INFINITE); //::CloseHandle(hThead); break; } } } } return FALSE; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG_MAIN),NULL,MainDialogProc); return 0; }
0则评论给“简单测试互斥体”