Library/winsock

[C] Critical Section

퓨림노 2009. 4. 20. 12:54
크리티컬 세션을 이용해서를 사용해서 1초를 구하는 것.!
#include "stdafx.h"
#include  //windows.h header
#include 
#include 

DWORD WINAPI MyThread(LPVOID temp);
CRITICAL_SECTION g_CriticalSection; //우선 크리티컨 세션을 설정후에... 전역변수

int main(int argc, char* argv[])
{

                  DWORD dwMyThreadID;
                  HANDLE lThread;
                  DWORD dwStart;
                  int nCnt = 10;

                  InitializeCriticalSection(&g_CriticalSection);//  초기화해주고
                  lThread = CreateThread(NULL, 0, MyThread, NULL, 0, &dwMyThreadID);


                  while(true)
                  {
                                   dwStart = GetTickCount(); //현재시간을 구하자
                                   //타이머 딜레이 주기
                                   while(GetTickCount() - dwStart < 1000);
                                   printf("\r\n 1초마다 실행됩니다.");
                  }
                  return 0;
}

DWORD WINAPI MyThread(LPVOID temp)
{    
  int tmp_t ;
  int kkk;

 while(true)
 {
 // dwStart = GetTickCount();
                  tmp_t = clock();
                  kkk = tmp_t + 2500;
  //2초당한번 출력한다
                  while( tmp_t <= kkk ){
                                   tmp_t = clock();
                  }
                                   EnterCriticalSection(&g_CriticalSection);
                                   printf("\r\n 2.5초마다 실행됩니다.");
                                   LeaveCriticalSection(&g_CriticalSection);//  크리티컬세션 작업 종료
                  }
}