2008. 10. 3. 22:11

[쓰레드기법] Bad Synchronization

출처 : 신경준씨 사외교육, http://blog.naver.com/process3/20052147545

#include "stdafx.h"
#include <windows.H>
#include <stdio.H>
#include <tchar.H>


#define LOOP_CNT 500000
int  nData = 0;

CRITICAL_SECTION CriticalSection;

void Produce(int &nData)
{
 nData++;
 Sleep(0);
}

void Consume(int &nData)
{
 nData--;
 Sleep(0);
}

DWORD CALLBACK ProduceThread(void* arg)
{
 int  nCnt = 0;
 BOOL bRun = TRUE;

 while(TRUE == bRun)
 {
  EnterCriticalSection(&CriticalSection);

  Produce(nData);
  nCnt++;
  if( LOOP_CNT == nCnt)
  {
   bRun = FALSE;
  }

  LeaveCriticalSection(&CriticalSection);

 }

 _tprintf(_T("ProduceThread %d\n"), nCnt);   

 return 0;
}

DWORD CALLBACK ConsumeThread(void* arg)
{
 int  nCnt = 0;
 BOOL bRun = TRUE;

 while(TRUE == bRun)
 {
  EnterCriticalSection(&CriticalSection);

  Consume(nData);
  nCnt++;
  if( LOOP_CNT == nCnt)
  {
   bRun = FALSE;
  }

  LeaveCriticalSection(&CriticalSection);
 }

 _tprintf(_T("ConsumeThread %d\n"), nCnt);   
 
 return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
 HANDLE hThread[2] = {NULL};

 InitializeCriticalSection(&CriticalSection);
  
 DWORD startTime = GetTickCount();   

 hThread[0] = CreateThread(NULL, 0, ProduceThread, (LPVOID)0, 0, NULL);   
 hThread[1] = CreateThread(NULL, 0, ConsumeThread, (LPVOID)0, 0, NULL);   
 
 WaitForMultipleObjects(2, hThread, TRUE, INFINITE);   

 _tprintf(_T("%d %d\n"), GetTickCount() - startTime, nData);   

 DeleteCriticalSection(&CriticalSection);

 return 0;
}

2008. 10. 3. 22:11

[쓰레드기법] Little good synchronization

출처 : 신경준씨 사외교육 http://blog.naver.com/process3/20052147545

#include "stdafx.h"
#include <windows.H>
#include <stdio.H>
#include <tchar.H>


#define LOOP_CNT 500000
int  nData = 0;

CRITICAL_SECTION CriticalSection;

void Produce(int &nData)
{
 nData++;
 Sleep(0);
}

void Consume(int &nData)
{
 nData--;
 Sleep(0);
}

DWORD CALLBACK ProduceThread(void* arg)
{
 int  nCnt = 0;
 BOOL bRun = TRUE;
 int  nTemp = 0;

 while(TRUE == bRun)
 {
  EnterCriticalSection(&CriticalSection);
  nTemp = nData;
  LeaveCriticalSection(&CriticalSection);

  Produce(nTemp);
  nCnt++;
  if( LOOP_CNT == nCnt)
  {
   bRun = FALSE;
  }

 }

 _tprintf(_T("ProduceThread %d\n"), nCnt);   

 return 0;
}

DWORD CALLBACK ConsumeThread(void* arg)
{
 int  nCnt = 0;
 BOOL bRun = TRUE;
 int  nTemp = 0;

 while(TRUE == bRun)
 {
  EnterCriticalSection(&CriticalSection);
  nTemp = nData;
  LeaveCriticalSection(&CriticalSection);

  Consume(nTemp);
  nCnt++;
  if( LOOP_CNT == nCnt)
  {
   bRun = FALSE;
  }
 }

 _tprintf(_T("ConsumeThread %d\n"), nCnt);   
 
 return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
 HANDLE hThread[2] = {NULL};

 InitializeCriticalSection(&CriticalSection);
  
 DWORD startTime = GetTickCount();   

 hThread[0] = CreateThread(NULL, 0, ProduceThread, (LPVOID)0, 0, NULL);   
 hThread[1] = CreateThread(NULL, 0, ConsumeThread, (LPVOID)0, 0, NULL);   
 
 WaitForMultipleObjects(2, hThread, TRUE, INFINITE);   

 _tprintf(_T("%d %d\n"), GetTickCount() - startTime, nData);   

 DeleteCriticalSection(&CriticalSection);

 return 0;
}

2008. 10. 3. 22:10

[쓰레드동기화기법] CPU_Locality_Example


#include "stdafx.h"
#include <windows.H>
#include <stdio.H>
#include <tchar.H>

volatile int data1;
volatile int data2;

DWORD CALLBACK TestThread1(void* arg)
{

 for (int i = 0; i < 500000000; ++i)       
  data1 = data1 + 1;

  
 return data1;
}

DWORD CALLBACK TestThread2(void* arg)
{  
 for (int i = 0; i < 500000000; ++i)       
  data2 = data2 + 1;  

  
 return data2;
}

int _tmain(int argc, _TCHAR* argv[])
{   
 HANDLE thread[2];   
 
 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);    
 
 DWORD startTime = GetTickCount();   
 
 thread[0] = CreateThread(NULL, 0, TestThread1, (LPVOID)0, 0, NULL);   
 thread[1] = CreateThread(NULL, 0, TestThread2, (LPVOID)0, 0, NULL);   
 
 WaitForMultipleObjects(2, thread, TRUE, INFINITE);   
 
 _tprintf(_T("%d\n"), GetTickCount() - startTime);   
 return 0;

}


출처 : 신경준씨 사외교육 http://blog.naver.com/process3/20052147545