There is two kinds of Synchronization:-
1.Synchronization in same process
eq.Critical section
2.Synchronizaiton in different process
eq. CEvent,Mutex,SemaPhore
1.Critical Section:-
It is same as a Mutex,but difference between them is Mutex can Synchronization different process where as Critical Section work in to the same process..Critical section is fastest Synchronization object compare of the other ,because it work in same process.
CRITICAL_SECTTION m_cs;
InitializeCriticalSection(&m_cs);
void threadfirst()
{
EnterCriticalSection(&m_cs);
........
.........
LeaveCriticalSection(&m_cs);
}
void threadSec()
{
EnterCriticalSection(&m_cs);
.......
.......
LeaveCriticalSection(&m_cs);
}
Critical Section Example:-
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <windows.h>
#include <iostream>
using namespace std;
CRITICAL_SECTION m_cs;
const int gcMax=10;
volatile int gcount=0;
void ThreadMain(char *name)
{
while (gcount<gcMax)
{
EnterCriticalSection(&m_cs);
printf(" %d i am %s this is my name \n ",gcount ,name);
gcount++;
Sleep(100);
LeaveCriticalSection(&m_cs);
}
}
int main()
{
HANDLE hT[4];
DWORD dWord;
InitializeCriticalSection(&m_cs);
cout<<"Starting......"<<endl;
Sleep(500);
hT[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"first",0,&dWord);
hT[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"second",0,&dWord);
hT[2]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"third",0,&dWord);
hT[3]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"forth",0,&dWord);
WaitForMultipleObjects(4,hT,TRUE,INFINITE);
cout<<"completed"<<endl;
DeleteCriticalSection(&m_cs);
return 0;
}
1.Synchronization in same process
eq.Critical section
2.Synchronizaiton in different process
eq. CEvent,Mutex,SemaPhore
1.Critical Section:-
It is same as a Mutex,but difference between them is Mutex can Synchronization different process where as Critical Section work in to the same process..Critical section is fastest Synchronization object compare of the other ,because it work in same process.
CRITICAL_SECTTION m_cs;
InitializeCriticalSection(&m_cs);
void threadfirst()
{
EnterCriticalSection(&m_cs);
........
.........
LeaveCriticalSection(&m_cs);
}
void threadSec()
{
EnterCriticalSection(&m_cs);
.......
.......
LeaveCriticalSection(&m_cs);
}
Critical Section Example:-
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <windows.h>
#include <iostream>
using namespace std;
CRITICAL_SECTION m_cs;
const int gcMax=10;
volatile int gcount=0;
void ThreadMain(char *name)
{
while (gcount<gcMax)
{
EnterCriticalSection(&m_cs);
printf(" %d i am %s this is my name \n ",gcount ,name);
gcount++;
Sleep(100);
LeaveCriticalSection(&m_cs);
}
}
int main()
{
HANDLE hT[4];
DWORD dWord;
InitializeCriticalSection(&m_cs);
cout<<"Starting......"<<endl;
Sleep(500);
hT[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"first",0,&dWord);
hT[1]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"second",0,&dWord);
hT[2]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"third",0,&dWord);
hT[3]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadMain,(LPVOID)"forth",0,&dWord);
WaitForMultipleObjects(4,hT,TRUE,INFINITE);
cout<<"completed"<<endl;
DeleteCriticalSection(&m_cs);
return 0;
}
Comments
Post a Comment