信号量,抢红包测试DEMO

// 20171228_01.cpp : Defines the entry point for the application.
//

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

HWND hEdit1;
HWND hEdit2;
HWND hEdit3;
HWND hEdit4;

HANDLE hThread1;
HANDLE hThread2;
HANDLE hThread3;
HANDLE hThread4;

HANDLE hSemaohore;
int TotalNum;

//EnterCriticalSection
CRITICAL_SECTION cs;

DWORD WINAPI ThreadProc2(
  LPVOID lpParameter   // thread data
)
{
	TCHAR numStr[10];
	memset(numStr,0,10);
	int num = 0;
	
	//开始执行线程
	WaitForSingleObject(hSemaohore,INFINITE);

	for(int i=0;i<100;i++)
	{
		GetWindowText(hEdit2,numStr,10);
		sscanf(numStr,"%d",&num);
		num++;
		
		EnterCriticalSection(&cs);
		//红包减少
		memset(numStr,0,10);
		GetWindowText(hEdit1,numStr,10);
		sscanf(numStr,"%d",&TotalNum);
		TotalNum--;
		memset(numStr,0,10);
		sprintf(numStr,"%d",TotalNum);
		SetWindowText(hEdit1,numStr);
		LeaveCriticalSection(&cs);

		Sleep(50);
		memset(numStr,0,10);
		sprintf(numStr,"%d",num);
		SetWindowText(hEdit2,numStr);
	}
	
	ReleaseSemaphore(hSemaohore,1,NULL);
	
	return 0;
}

DWORD WINAPI ThreadProc3(
  LPVOID lpParameter   // thread data
)
{
	TCHAR numStr[10];
	memset(numStr,0,10);
	int num = 0;

	//开始执行线程
	WaitForSingleObject(hSemaohore,INFINITE);

	for(int i=0;i<100;i++)
	{
		GetWindowText(hEdit3,numStr,10);
		sscanf(numStr,"%d",&num);
		num++;

		//红包减少
		EnterCriticalSection(&cs);
		memset(numStr,0,10);
		GetWindowText(hEdit1,numStr,10);
		sscanf(numStr,"%d",&TotalNum);
		TotalNum--;
		memset(numStr,0,10);
		sprintf(numStr,"%d",TotalNum);
		SetWindowText(hEdit1,numStr);
		LeaveCriticalSection(&cs);


		Sleep(50);
		memset(numStr,0,10);
		sprintf(numStr,"%d",num);
		SetWindowText(hEdit3,numStr);
	}
	
	ReleaseSemaphore(hSemaohore,1,NULL);
	
	return 0;
}

DWORD WINAPI ThreadProc4(
  LPVOID lpParameter   // thread data
)
{
	TCHAR numStr[10];
	memset(numStr,0,10);
	int num = 0;
	
	//开始执行线程
	WaitForSingleObject(hSemaohore,INFINITE);

	for(int i=0;i<100;i++)
	{
		GetWindowText(hEdit4,numStr,10);
		sscanf(numStr,"%d",&num);
		num++;

		//红包减少
		EnterCriticalSection(&cs);
		memset(numStr,0,10);
		GetWindowText(hEdit1,numStr,10);
		sscanf(numStr,"%d",&TotalNum);
		TotalNum--;
		memset(numStr,0,10);
		sprintf(numStr,"%d",TotalNum);
		SetWindowText(hEdit1,numStr);
		LeaveCriticalSection(&cs);


		Sleep(50);
		memset(numStr,0,10);
		sprintf(numStr,"%d",num);
		SetWindowText(hEdit4,numStr);
	}
	
	ReleaseSemaphore(hSemaohore,1,NULL);
	
	
		
	return 0;
}



DWORD WINAPI MainThreadProc(
  LPVOID lpParameter   // thread data
)
{
	TCHAR numStr[10];
	memset(numStr,0,10);
	int num = 0;
	
	HANDLE hThread[3];
	
	//创建信号量
	hSemaohore = CreateSemaphore(NULL,0,3,NULL);

	hThread[0] = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
	hThread[1] = CreateThread(NULL,0,ThreadProc3,NULL,0,NULL);
	hThread[2] = CreateThread(NULL,0,ThreadProc4,NULL,0,NULL);

	//Edit1 的文本内容设置
	for(int i=0;i<1000;i++)
	{
		GetWindowText(hEdit1,numStr,10);
		sscanf(numStr,"%d",&num);
		num++;
		Sleep(10);
		memset(numStr,0,10);
		sprintf(numStr,"%d",num);
		SetWindowText(hEdit1,numStr);
	}

	//开始执行两个
	//CreateSemaphore(NULL,)
	ReleaseSemaphore(hSemaohore,2,NULL);
	
	WaitForMultipleObjects(3,hThread,TRUE,INFINITE);

	CloseHandle(hThread[0]);
	CloseHandle(hThread[1]);
	CloseHandle(hThread[2]);
	CloseHandle(hSemaohore);

	//MessageBox(0,0,0,0);
	return 0;
}


BOOL CALLBACK MainDialogFunc(
  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);

			hEdit1 = GetDlgItem(hwndDlg,IDC_EDIT1);
			hEdit2 = GetDlgItem(hwndDlg,IDC_EDIT2);
			hEdit3 = GetDlgItem(hwndDlg,IDC_EDIT3);
			hEdit4 = GetDlgItem(hwndDlg,IDC_EDIT4);
			
			SetWindowText(hEdit1,"0");
			SetWindowText(hEdit2,"0");
			SetWindowText(hEdit3,"0");
			SetWindowText(hEdit4,"0");

			break;
		}

	case WM_CLOSE:
		{
			DeleteCriticalSection(&cs);
			EndDialog(hwndDlg, 0);
			break;
		}

	case WM_COMMAND:
		{
			switch(LOWORD(wParam))
			{
			case IDC_BUTTON_BEGIN:
				{
					//MessageBox(0,0,0,0);
					//开启主线程
					hThread1 = CreateThread(NULL,0,MainThreadProc,NULL,0,NULL);
					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,MainDialogFunc);
	
	
	
	return 0;
}



原文链接: 信号量,抢红包测试DEMO 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-353.html )

发表评论

0则评论给“信号量,抢红包测试DEMO”