C++ EnterCriticalSection简单使用

  // DefDemo.cpp : 定义控制台应用程序的入口点。

  //

  #include "stdafx.h"

  using namespace std;

  CRITICAL_SECTION g_cs;

  struct gParam

  {

  int m;

  int n;

  };

  void WINAPI getMax(gParam * cur_Param)

  {

  EnterCriticalSection(&g_cs);

  if (cur_Param->m > cur_Param->n)

  {

  cout << "MAX_VALUE:"<< cur_Param->m << endl;

  }

  else

  {

  cout << "MAX_VALUE:"<< cur_Param->n << endl;

  }

  LeaveCriticalSection(&g_cs);

  return;

  }

  int _tmain(int argc, _TCHAR* argv[])

  {

  InitializeCriticalSection(&g_cs);

  gParam * cur_Param = new gParam;

  cur_Param->m = 2;

  cur_Param->n = 5;

  gParam * cur_NexParam = new gParam;

  cur_NexParam->m = 10;

  cur_NexParam->n = 2;

  CreateThread(nullptr,0,(LPTHREAD_START_ROUTINE)getMax,cur_Param,0,0);

  CreateThread(nullptr,0,(LPTHREAD_START_ROUTINE)getMax,cur_NexParam,0,0);

  system("pause");

  delete cur_Param;

  return 0;

  }