'Operations Dev/writech'에 해당되는 글 6건

  1. 2010.12.10 Mutex
  2. 2010.12.10 Semaphore
  3. 2010.12.10 CriticalSection
  4. 2010.12.10 SpinLock
  5. 2010.12.10 SpinLock2
  6. 2010.12.10 SimpleConcurrency
posted by Kyleslab 2010. 12. 10. 18:24


// Example02_Mutex.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include "Windows.h"
#include <process.h>

//#include <concrt.h>

#include <string>
#include <iostream>
using namespace std;

unsigned int WINAPI writeCharacter( LPVOID param);

string g_string;
int const MAX_THREAD = 5;
HANDLE g_mutexHandle;

int _tmain(int argc, _TCHAR* argv[])
{
 //가용 공간을 미리 예약해 놓는 부분,
 //g_string.reserve( MAX_THREAD * 1000 +1 );

 g_mutexHandle = ::CreateMutexA( NULL, FALSE, NULL);

 //char ch = 'a';
 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i=0; i < MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;
  threadHandles[i] = (HANDLE)::_beginthreadex( NULL,
   0,
   writeCharacter,
   (LPVOID*)(&alphas[i]), // LPVOID 포인터 타입 무슨 타입인지알아야한다. 이것은 char타입니다.
   0,
   (unsigned int *)&threadIDs[i]);

 }

 DWORD result =
  ::WaitForMultipleObjects(MAX_THREAD,
  &threadHandles[0],
  TRUE,
  INFINITE);

 //결과값이 제대로 나온다면
 if(result == WAIT_OBJECT_0)
 {
  cout<<g_string;
 }

 //::ReleaseMutex(g_mutexHandle);

 for(int i=0; i < MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }
 CloseHandle( g_mutexHandle);
 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char*)param);

 DWORD result = ::WaitForSingleObject(g_mutexHandle,INFINITE); // 대기시간 무한!

 for(int i = 0; i < 1000; ++i)
 {
  g_string += ch;
 }

 ::ReleaseMutex(g_mutexHandle);

 return 0;
}

'Operations Dev > writech' 카테고리의 다른 글

Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SpinLock2  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 18:23

// Example03_Semaphore.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;
HANDLE g_semaphoreHandle;

int _tmain(int argc, _TCHAR* argv[])
{
 g_semaphoreHandle = ::CreateSemaphore(NULL,1, 1 , NULL);
 g_string.reserve(MAX_THREAD * 1000 +1);  //가용공간 예약부분

 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result = ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE);
 if(result == WAIT_OBJECT_0)
 {
  
  cout<<g_string;
  
 }

 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }
 CloseHandle(g_semaphoreHandle);

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 DWORD result = ::WaitForSingleObject(g_semaphoreHandle,INFINITE);
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 ::ReleaseSemaphore(g_semaphoreHandle,1,NULL);
 
 return 0;
}


 

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SpinLock2  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 18:22

// Example04_CriticalSection.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
// CriticalSection은 유저모드(재귀 스핀락)인데 오늘은 일반 락

#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;
CRITICAL_SECTION  g_criticalSection;

int _tmain(int argc, _TCHAR* argv[])
{
 ::InitializeCriticalSection(&g_criticalSection);

 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result = ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE);
 if(result == WAIT_OBJECT_0)
 {
  
  cout<<g_string;
  
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }
 ::DeleteCriticalSection(&g_criticalSection);

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 ::EnterCriticalSection(&g_criticalSection);
 /***************임계영역 *************************/
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 /**************************************************/
 ::LeaveCriticalSection(&g_criticalSection);
 
 return 0;
}

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SpinLock2  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 18:22


// Example05_SpinLock.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
// 커널모르게 돌리면 유저모드

#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;
bool g_isLocked = false;

int _tmain(int argc, _TCHAR* argv[])
{
 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result = ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE);
 if(result == WAIT_OBJECT_0)
 {
  
  cout<<g_string;
  
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 
 while(g_isLocked); 
 g_isLocked = true;   //이곳에서 ContextSwitching이 일어나면 위의 while문에서 뻑날수있다. 원자적연산이 아니기때문에.
 /***************임계영역 *************************/
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 /**************************************************/
 g_isLocked = false;
 
 
 return 0;
}

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock2  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 18:10

// Example06_SpinLock2.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;
long g_key =1;

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

 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result = ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE);
 if(result == WAIT_OBJECT_0)
 {
  
  cout<<g_string;
  
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 
 LONG key = g_key;
 while(1 != ::InterlockedCompareExchange(&g_key, 0 ,1));  //( 조건 ? 참 : 거짓)과 같다
 /***************임계영역 *************************/
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 /**************************************************/
 g_key = 1; 
 
 return 0;
}

 

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SimpleConcurrency  (0) 2010.12.10
posted by Kyleslab 2010. 12. 10. 17:53

// Example01_Simple.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

#include<windows.h>
#include<process.h>

#include<string>
#include<iostream>
using namespace std;

unsigned int WINAPI writeCharacter(LPVOID param);

string g_string;
int const MAX_THREAD = 5;

int _tmain(int argc, _TCHAR* argv[])
{
 g_string.reserve(MAX_THREAD * 1000 +1);  //가용공간 예약부분

 //char ch = 'a' ;
 DWORD threadIDs[MAX_THREAD];
 HANDLE threadHandles[MAX_THREAD];
 char alphas[MAX_THREAD];

 for(int i =0; i<MAX_THREAD; ++i)
 {
  alphas[i] = 'a' + i;

  threadHandles[i] = (HANDLE) ::_beginthreadex( NULL, 0 , writeCharacter,(LPVOID*)(&alphas[i]),0,(unsigned int *)&threadIDs[i]);
 }
 
 DWORD result =
  ::WaitForMultipleObjects(MAX_THREAD, &threadHandles[0],TRUE,INFINITE); //블록상태로 전환시킨다. 이 커널Object가 사용가능할 때까지
                 //대기시간 무한
 if(result == WAIT_OBJECT_0)  //대기가 끝난후 결과가 나오면 출력
 {
  cout<<g_string;
 }
 for(int i = 0; i< MAX_THREAD; ++i)
 {
  CloseHandle(threadHandles[i]);
 }

 return 0;
}

unsigned int WINAPI writeCharacter(LPVOID param)
{
 char ch = *((char *)param);
 for(int i = 0; i< 1000; ++i)
 {
  g_string += ch;
 }
 
 return 0;
}

'Operations Dev > writech' 카테고리의 다른 글

Mutex  (0) 2010.12.10
Semaphore  (0) 2010.12.10
CriticalSection  (0) 2010.12.10
SpinLock  (0) 2010.12.10
SpinLock2  (0) 2010.12.10