windows下 c 实现生产者消费者问题

#include 
#include 

#define P(S) WaitForSingleObject(S, INFINITE) //定义Windows下的P操作
#define V(S) ReleaseSemaphore(S, 1, NULL)     //定义Windows下的V操作

//生产者 消费者 缓冲区
#define num_of_productors 2
#define num_of_consumers 3
#define num_of_buffers 3

int nextp = 1;                      //下一个产品的编号
int in = 0, out = 0;                //分别指向下一个存放产品和取出产品的缓冲区编号
int g_Buffer[num_of_buffers] = {0}; //缓冲区初始值为空

HANDLE g_hSemaphoreEmpty, g_hSemaphoreFull, g_hMutex;

//生产者线程函数
DWORD WINAPI ProducerThreadFun(LPVOID pM)
{
  Sleep(100);
  do
  {
    int id = *((int *)(pM)); //生产者编号
    P(g_hSemaphoreEmpty);
    P(g_hMutex);
    g_Buffer[in] = nextp;
    printf("生产者%d将数据%d放入缓冲区%d\n", id, nextp, in);
    nextp++;
    in = (in + 1) % num_of_buffers;
    V(g_hMutex);
    V(g_hSemaphoreFull);
    Sleep(100);
    if (nextp > 12)
      break;
  } while (true);
  return 0;
}
//消费者线程函数
DWORD WINAPI ConsumerThreadFun(LPVOID pM)
{
  Sleep(100);
  do
  {
    int id = *((int *)(pM)); //消费者编号
    P(g_hSemaphoreFull);
    P(g_hMutex);
    //nextp = g_Buffer[out];
    printf(" 消费者%d从缓冲区%d中取数据%d\n", id, out, g_Buffer[out]);
    out = (out + 1) % num_of_buffers;
    V(g_hMutex);
    V(g_hSemaphoreEmpty);
    Sleep(100);
  } while (true);
}
int main()
{
  int num[num_of_productors + num_of_consumers];
  printf(" 生产者消费者问题:%d生产者 %d消费者 %d缓冲区\n", num_of_productors, num_of_consumers, num_of_buffers);
  //初始值为num_of_buffers的信号量
  g_hSemaphoreEmpty = CreateSemaphore(NULL, num_of_buffers, num_of_buffers, NULL); //继承/初始化信号计数/最大信号计数/名称
  //初始值为0的信号量
  g_hSemaphoreFull = CreateSemaphore(NULL, 0, num_of_buffers, NULL);
  //互斥量
  g_hMutex = CreateSemaphore(NULL, 1, 1, NULL);

  const int THREADNUM = num_of_productors + num_of_consumers; //线程数
  HANDLE hThread[THREADNUM];

  for (int i = 0; i < num_of_productors; i++) //创建生产者线程
  {
    num[i] = i + 1;
    hThread[i] = CreateThread(NULL, 0, ProducerThreadFun, (LPVOID *)&num[i], 0, NULL);
  }                                                                              //继承/初始栈大小/线程函数/向线程函数传递的参数/线程标志 0 表示创建后立即激活/保存新线程的ID
  for (int i = num_of_productors; i < num_of_productors + num_of_consumers; i++) //创建消费者者线程
  {
    num[i] = i - num_of_productors + 1;
    hThread[i] = CreateThread(NULL, 0, ConsumerThreadFun, (LPVOID *)&num[i], 0, NULL);
  }

  WaitForMultipleObjects(THREADNUM, hThread, TRUE, INFINITE);

  system("pause");
  return 0;
}

你可能感兴趣的:(windows下 c 实现生产者消费者问题)