I am assuming that you mean syncronising access to data where threads could read manipulate and replace data without the threat of another thread changing it before it has finished manipulating.....
Ok....I think I managed an example, but I admit I fount it hard to show a simple example where thread sync was needed........therefore I may have gone over the top to prove a point
Anyway......I wrote a program that fills a buffer with the 26 letter of the alphabet......it the shows that buffer in a message box.......the buffer is filled by 2 threads working at exactly the same time doing the same thing.......now....the program needs to ensure that while 1 thread is filling 1 member of the array the other thread does not do anything to cause the first threads efforts to be spoiled...
With critical sections.....when 1 thread has access to data, it is guaranteed that another thread cannot manipulate it.........if a second thread tries to access that variable, then the second thread is put to sleep until the first thread has done what it wanted...the second thread is then preempted and is allowed to do what it wants........
Code:
#include <windows.h>
int nIncrement = 0; //increment thats used by both threads
bool bThread1 = false,//A little test to see if both threads..
bThread2 = false;//..recieved CPU time
char buffer[27];//Buffer to fill with alphabet
CRITICAL_SECTION cs;//Critical section that syncs threads
DWORD WINAPI ThreadProc1(void){
bool bLoop = true;
char cAlpha;
while(bLoop){
EnterCriticalSection(&cs);
/*Add capital letter to buffer*/
cAlpha = nIncrement+65;
Sleep(20);//To stall thread execution
buffer[nIncrement] = cAlpha;
Sleep(20);//To stall thread execution
nIncrement++;//Increment counter
bThread1 = true;//To show that this thread was used
if(nIncrement>=25)bLoop = false;//is the buffer full?
LeaveCriticalSection(&cs);
}
return 0;
}
DWORD WINAPI ThreadProc2(void){
bool bLoop = true;
char cAlpha;
while(bLoop){
EnterCriticalSection(&cs);
/*Add capital letter to buffer*/
cAlpha = nIncrement+65;
Sleep(20);//To stall thread execution
buffer[nIncrement] = cAlpha;
Sleep(20);//To stall thread execution
nIncrement++;//Increment counter
bThread2 = true;//To show that this thread was used
if(nIncrement>=25)bLoop = false;//is the buffer full?
LeaveCriticalSection(&cs);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, int nFunsterStil)
{
DWORD dwThread1,dwThread2;//Thread ID's (not used)
HANDLE hThreads[2];//Thread handles
InitializeCriticalSection(&cs);//Setup the Critical Section
hThreads[0] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE )
ThreadProc1,NULL,0,&dwThread1);//Start thread 1
hThreads[1] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE )
ThreadProc2,NULL,0,&dwThread2);//Start thread 2
/*Hold execution until threads have finished*/
WaitForMultipleObjects(2,hThreads,TRUE,INFINITE);
DeleteCriticalSection(&cs);//Cleanup
buffer[27] = NULL;//Null the end of the buffer
MessageBox(HWND_DESKTOP,buffer,"Results",MB_OK);//Display
/*Test to show that each thread recieved CPU time
and did affect the buffer*/
if(bThread1)
MessageBox(HWND_DESKTOP,"Thread 1 was used","",MB_OK);
if(bThread2)
MessageBox(HWND_DESKTOP,"Thread 2 was used","",MB_OK);
return 0;
}
Now this should do as it was meant to do....but if you comment the EnterCriticalSection() and the LeaveCriticalSection() in each thread procedure then you may find the program will not do as it should..(On mine, the first thread put 'A' in the buffer and that's all)...but if you uncomment the funcs again it will work....
I admit I probably went overboard with the Sleep()s....but it helped prove the point in a short program....sorry if this is not as good or as clear an example as it could be....its just that its not an easy method to give an example of.