From f1e2de8edd5d4aff802dcc7c4801e4834f51e8ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A7=9C=E6=B5=B7=E5=A4=A9?= <1076168468@qq.com>
Date: Tue, 25 Oct 2022 14:56:16 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8pc.c?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 EOSApp.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 165 insertions(+), 6 deletions(-)

diff --git a/EOSApp.c b/EOSApp.c
index d8f2bae..d9756f6 100644
--- a/EOSApp.c
+++ b/EOSApp.c
@@ -1,5 +1,36 @@
+/*
+Ìá¹©¸ÃÊ¾Àý´úÂëÊÇÎªÁË²ûÊÍÒ»¸ö¸ÅÄî£¬»òÕß½øÐÐÒ»¸ö²âÊÔ£¬²¢²»´ú±í×Å
+×î°²È«µÄ±àÂëÊµ¼ù£¬Òò´Ë²»Ó¦ÔÚÓ¦ÓÃ³ÌÐò»òÍøÕ¾ÖÐÊ¹ÓÃ¸ÃÊ¾Àý´úÂë¡£¶Ô
+ÓÚ³¬³ö±¾Ê¾Àý´úÂëµÄÔ¤ÆÚÓÃÍ¾ÒÔÍâµÄÊ¹ÓÃËùÔì³ÉµÄÅ¼È»»ò¼Ì·¢ÐÔËðÊ§£¬
+±±¾©Ó¢ÕæÊ±´ú¿Æ¼¼ÓÐÏÞ¹«Ë¾²»³Ðµ£ÈÎºÎÔðÈÎ¡£
+*/
+
 #include "EOSApp.h"
 
+//
+// »º³å³Ø¡£
+//
+#define BUFFER_SIZE		10
+int Buffer[BUFFER_SIZE];
+
+//
+// ²úÆ·ÊýÁ¿¡£
+//
+#define PRODUCT_COUNT	30
+
+//
+// ÓÃÓÚÉú²úÕßºÍÏû·ÑÕßÍ¬²½µÄ¶ÔÏó¾ä±ú¡£
+//
+HANDLE MutexHandle;
+HANDLE EmptySemaphoreHandle;
+HANDLE FullSemaphoreHandle;
+
+//
+// Éú²úÕßºÍÏû·ÑÕßµÄÏß³Ìº¯Êý
+//
+ULONG Producer(PVOID Param);
+ULONG Consumer(PVOID Param);
+
 //
 // main º¯Êý²ÎÊýµÄÒâÒå£º
 // argc - argv Êý×éµÄ³¤¶È£¬´óÐ¡ÖÁÉÙÎª 1£¬argc - 1 ÎªÃüÁîÐÐ²ÎÊýµÄÊýÁ¿¡£
@@ -13,14 +44,142 @@
 int main(int argc, char* argv[])
 {
 	//
-	// Èç¹ûÐèÒªÔÚµ÷ÊÔÓ¦ÓÃ³ÌÐòÊ±ÄÜ¹»µ÷ÊÔ½øÈëÄÚºË²¢ÏÔÊ¾¶ÔÓ¦µÄÔ´Âë£¬
-	// ±ØÐëÊ¹ÓÃ EOS ÄÚºËÏîÄ¿±àÒëÉú³ÉÍêÈ«°æ±¾µÄ SDK ÎÄ¼þ¼Ð£¬È»
-	// ºóÊ¹ÓÃ¸Õ¸ÕÉú³ÉµÄ SDK ÎÄ¼þ¼Ð¸²¸Ç´ËÓ¦ÓÃ³ÌÐòÏîÄ¿ÖÐµÄ SDK ÎÄ¼þ
-	// ¼Ð£¬²¢ÇÒ EOS ÄÚºËÏîÄ¿ÔÚ´ÅÅÌÉÏµÄÎ»ÖÃ²»ÄÜ¸Ä±ä¡£
+	// Æô¶¯µ÷ÊÔ EOS Ó¦ÓÃ³ÌÐòÇ°ÒªÌØ±ð×¢ÒâÏÂÃæµÄÎÊÌâ£º
+	//
+	// 1¡¢Èç¹ûÒªÔÚµ÷ÊÔÓ¦ÓÃ³ÌÐòÊ±ÄÜ¹»µ÷ÊÔ½øÈëÄÚºË²¢ÏÔÊ¾¶ÔÓ¦µÄÔ´Âë£¬
+	//    ±ØÐëÊ¹ÓÃ EOS ºËÐÄÏîÄ¿±àÒëÉú³ÉÍêÈ«°æ±¾µÄ SDK ÎÄ¼þ¼Ð£¬È»
+	//    ºóÊ¹ÓÃ´ËÎÄ¼þ¼Ð¸²¸ÇÓ¦ÓÃ³ÌÐòÏîÄ¿ÖÐµÄ SDK ÎÄ¼þ¼Ð£¬²¢ÇÒ EOS
+	//    ºËÐÄÏîÄ¿ÔÚ´ÅÅÌÉÏµÄÎ»ÖÃ²»ÄÜ¸Ä±ä¡£
+	//
+	
+	HANDLE ProducerHandle;
+	HANDLE ConsumerHandle;
+
+	//
+	// ´´½¨ÓÃÓÚ»¥³â·ÃÎÊ»º³å³ØµÄ Mutex ¶ÔÏó¡£
+	//
+	MutexHandle = CreateMutex(FALSE, NULL);
+	if (NULL == MutexHandle) {
+		return 1;
+	}
+
+	//
+	// ´´½¨ Empty ÐÅºÅÁ¿£¬±íÊ¾»º³å³ØÖÐ¿Õ»º³åÇøÊýÁ¿¡£³õÊ¼¼ÆÊýºÍ×î´ó¼ÆÊý¶¼Îª BUFFER_SIZE¡£
+	//
+	EmptySemaphoreHandle = CreateSemaphore(BUFFER_SIZE, BUFFER_SIZE, NULL);
+	if (NULL == EmptySemaphoreHandle) {
+		return 2;
+	}
+
+	//
+	// ´´½¨ Full ÐÅºÅÁ¿£¬±íÊ¾»º³å³ØÖÐÂú»º³åÇøÊýÁ¿¡£³õÊ¼¼ÆÊýÎª 0£¬×î´ó¼ÆÊýÎª BUFFER_SIZE¡£
+	//
+	FullSemaphoreHandle = CreateSemaphore(0, BUFFER_SIZE, NULL);
+	if (NULL == FullSemaphoreHandle) {
+		return 3;
+	}
+
+	//
+	// ´´½¨Éú²úÕßÏß³Ì¡£
+	//
+	ProducerHandle = CreateThread( 0,			// Ä¬ÈÏ¶ÑÕ»´óÐ¡
+								   Producer,	// Ïß³Ìº¯ÊýÈë¿ÚµØÖ·
+								   NULL,		// Ïß³Ìº¯Êý²ÎÊý
+								   0,			// ´´½¨±êÖ¾
+								   NULL );		// Ïß³Ì ID
+
+	if (NULL == ProducerHandle) {
+		return 4;
+	}
+
 	//
+	// ´´½¨Ïû·ÑÕßÏß³Ì¡£
+	//
+	ConsumerHandle = CreateThread( 0,
+								   Consumer,
+								   NULL,
+								   0,
+								   NULL );
+
+	if (NULL == ConsumerHandle) {
+		return 5;
+	}
+
+	//
+	// µÈ´ýÉú²úÕßÏß³ÌºÍÏû·ÑÕßÏß³Ì½áÊø¡£
+	//
+	WaitForSingleObject(ProducerHandle, INFINITE);
+	WaitForSingleObject(ConsumerHandle, INFINITE);
+	
+	//
+	// ¹Ø±Õ¾ä±ú
+	//
+	CloseHandle(MutexHandle);
+	CloseHandle(EmptySemaphoreHandle);
+	CloseHandle(FullSemaphoreHandle);
+	CloseHandle(ProducerHandle);
+	CloseHandle(ConsumerHandle);
+
+	return 0;
+}
+
+//
+// Éú²úÕßÏß³Ìº¯Êý¡£
+//
+ULONG Producer(PVOID Param) 
+{
+	int i;
+	int InIndex = 0;
+
+	for (i = 0; i < PRODUCT_COUNT; i++) {
+
+		WaitForSingleObject(EmptySemaphoreHandle, INFINITE);
+		WaitForSingleObject(MutexHandle, INFINITE);
+
+		printf("Produce a %d\n", i);
+		Buffer[InIndex] = i;
+		InIndex = (InIndex + 1) % BUFFER_SIZE;
+
+		ReleaseMutex(MutexHandle);
+		ReleaseSemaphore(FullSemaphoreHandle, 1, NULL);
+
+		//
+		// ÐÝÏ¢Ò»»á¡£Ã¿ 500 ºÁÃëÉú²úÒ»¸öÊý¡£
+		//
+		Sleep(500);
+	}
+	
+	return 0;
+}
+
+//
+// Ïû·ÑÕßÏß³Ìº¯Êý¡£
+//
+ULONG Consumer(PVOID Param)
+{
+	int i;
+	int OutIndex = 0;
+
+	for (i = 0; i < PRODUCT_COUNT; i++) {
+
+		WaitForSingleObject(FullSemaphoreHandle, INFINITE);
+		WaitForSingleObject(MutexHandle, INFINITE);
+
+		printf("\t\t\tConsume a %d\n", Buffer[OutIndex]);
+		OutIndex = (OutIndex + 1) % BUFFER_SIZE;
 
-	/* TODO: ÔÚ´Ë´¦Ìí¼Ó×Ô¼ºµÄ´úÂë */
-	printf("Hello world!\n");
+		ReleaseMutex(MutexHandle);
+		ReleaseSemaphore(EmptySemaphoreHandle, 1, NULL);
 
+		//
+		// ÐÝÏ¢Ò»»á¶ù¡£ÈÃÇ° 10 ¸öÊýµÄÏû·ÑËÙ¶È±È½ÏÂý£¬ºóÃæµÄ½Ï¿ì¡£
+		//
+		if (i < 10) {
+			Sleep(2000);
+		} else {
+			Sleep(100);
+		}
+	}
+	
 	return 0;
 }
-- 
2.34.1.windows.1

