메뉴 바로가기

서브메뉴 바로가기

본문 바로가기

logo

C/C++/MFC

쓰레드 - 크리티컬섹션

2007.01.15 10:17

WhiteAT 조회 수:10570

// test1.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <windows.h> //windows.h header

#include <iostream>

 

 

#include <time.h>

 

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)

{

// DWORD dwStart;

                  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);//  크리티컬세션 작업 종료

 

                  }

}