Windows API 线程同步 生产者消费者

/*第一次使用windows API线程函数,还望给位大虾指教啊*/

#include <windows.h>
#include <stdio.h>

#define BUFFER_SIZE 10

HANDLE hSemaphore_Full;
HANDLE hSemaphore_Empty;
HANDLE hMutex;
HANDLE hThread[2];
CHAR lpSharedBuffer[BUFFER_SIZE] = {0};

DWORD WINAPI ProducerThread(LPVOID lpParam);
DWORD WINAPI ConsumerThread(LPVOID lpParam);

void UseMutex(void);
void UseSemaphore(void);
void UseThread(void);

int main(void)
{
 UseSemaphore();
 UseMutex();
 UseThread();
 printf("\n\n");
 DWORD i;
 WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
 {
  for (i = 0; i < 2; i++)
  {
   CloseHandle(hThread[i]);
  }
 }
 printf("\n");
 return 0;
}

DWORD WINAPI ProduceThread(LPVOID lpParam)
{
 DWORD dwWaitResult_Semaphore;
 DWORD dwWaitResult_Mutex;
 LONG dwPreviousCount;
 DWORD j = 0;
 for(;j < 20; j++){

 Sleep(rand()%1000);

 dwWaitResult_Semaphore = WaitForSingleObject(hSemaphore_Empty,
                              INFINITE
            );
 switch (dwWaitResult_Semaphore)
 {
 case WAIT_OBJECT_0:
//#ifdef MUTEX
  dwWaitResult_Mutex = WaitForSingleObject(hMutex,
                                 INFINITE);
  switch (dwWaitResult_Mutex)
  {
  case WAIT_OBJECT_0:
//#endif
      printf("ProduceProcess %d Gets Semaphore!\n",GetCurrentThreadId());
//#ifdef MUTEX
   if (!ReleaseMutex(hMutex))
   {
    printf("Relase Produce Mutex error : %d\n",GetLastError());
   }
   else
   {
    printf("Produce Mutex has been Relased!\n");
   }
   break;
  default:
   printf(" Produce Mutex Wait error: %d\n",GetLastError());
  }
//#endif
  break;
 default:
  printf("ProduceProcess %u Wait error: %u \n",GetLastError());
 }

 Sleep(rand()%1000);

 if (!ReleaseSemaphore(hSemaphore_Full,
                 1,
        &dwPreviousCount) )
 {
  printf("ProduceProcess %u ReleaseSemaphore error: %d \n",
       GetCurrentThreadId());

 }
 else
 {
  printf("ProduceProcess %u release Semaphore,previous count is %u \n",
      GetCurrentThreadId(),
      dwPreviousCount);
 }
 }
 return 1;
}

DWORD WINAPI ConsumerThread(LPVOID lpParam)
{
 DWORD dwWaitResult_Semaphore;
 DWORD dwWaitResult_Mutex;
 LONG dwPreviousCount;
 DWORD j=0;

 for(;j < 20; j++){
 Sleep(rand()%1000);

 dwWaitResult_Semaphore = WaitForSingleObject(hSemaphore_Full,
                              INFINITE
            );
 switch (dwWaitResult_Semaphore)
 {
 case WAIT_OBJECT_0:
//#ifdef MUTEX
  dwWaitResult_Mutex = WaitForSingleObject(hMutex,
                                 INFINITE);
  switch (dwWaitResult_Mutex)
  {
  case WAIT_OBJECT_0:
//#endif
   //访问临界区
   printf("ConsumerProcess %d Gets Semaphore!\n",GetCurrentThreadId());
//#ifdef MUTEX
   if (!ReleaseMutex(hMutex))
   {
    printf("Consumer Relase Mutex error : %d\n",GetLastError());
   }
   else
   {
    printf("Consumer Mutex has been Relased!\n");
   }
   break;
  default:
   printf("Consumer mutex Wait error: %d\n",GetLastError());
  }
//#endif
  break;
 default:
  printf("ConsumerProcess %u Wait error: %u \n",GetLastError());
 }

 Sleep(rand()%1000);

 if (!ReleaseSemaphore(hSemaphore_Empty,
                 1,
        &dwPreviousCount) )
 {
  printf("ConsumerProcess %u ReleaseSemaphore error: %d \n",
       GetCurrentThreadId());

 }
 else
 {
  printf("ConsumerProcess %u release Semaphore,previous count is %u \n",
      GetCurrentThreadId(),
      dwPreviousCount);
 }
 }
 return 1;
}

void UseMutex(void)
{
//#ifdef MUTEX
 hMutex = CreateMutex(NULL,
                   FALSE,
       NULL);
 if (hMutex == NULL)
 {
  printf("CreateMutex error: %d\n",GetLastError());
  ExitProcess(0);
 }
 else
 {
  printf("Mutex has been Created!\n");
 }
//#endif
}

void UseSemaphore(void)
{
 hSemaphore_Full = CreateSemaphore(NULL,
                                0,//初始化计数器
              BUFFER_SIZE,//最大计数
              NULL//名字
              );
 if (hSemaphore_Full == NULL)
 {
  printf("CreateSemaphore_Full error: %d\n",GetLastError());
  ExitProcess(0);
 }
 else
 {
  printf("Semaphore_Full has been Created!\n");
 }
 hSemaphore_Empty = CreateSemaphore(NULL,
                                BUFFER_SIZE,//初始化计数器
              BUFFER_SIZE,//最大计数
              NULL//名字
              );
 if (hSemaphore_Empty == NULL)
 {
  printf("CreateSemaphore_Empty error: %d\n",GetLastError());
  ExitProcess(0);
 }
 else
 {
  printf("Semaphore_Empty has been Created!\n");
 }
}

void UseThread(void)
{
 hThread[0] = CreateThread(NULL,
                     0,
         ProduceThread,
         NULL,
         0,
         NULL);
 if (hThread[0] == NULL)
 {
  printf("Create ProduceThread error! (%d)\n",GetLastError());
  ExitProcess(0);
 }
 else
 {
  printf("ProduceThread has been Created!\n");
 }
 hThread[1] = CreateThread(NULL,
                     0,
         ConsumerThread,
         NULL,
         0,
         NULL);
 if (hThread[1] == NULL)
 {
  printf("Create ConsumerThread error! (%d)\n",GetLastError());
  ExitProcess(0);
 }
 else
 {
  printf("ConsumerThread has been Created!\n");
 }
}

转载于:https://my.oschina.net/u/251487/blog/48684

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值