Implementing simple multithreading in C++

Bare steps to implement multi threading in C++:
a. Declare a global variable, to hold the reference counts.
b. Declare CRITICAL SECTION
code:
CRITICAL_SECTION cs_test;
int loadCnt=0;
c. Now everytime you access the code you want to be protected when multiple threads access them
InitializeCriticalSection(&cs_test);
EnterCriticalSection(&cs_test);
if(loadCnt >0)
{
loadCnt++;
return
}else{
//your code here
}
LeaveCriticalSection(&cs_test);
return;
d. you could do the same for de initializing also.
e. But the best way to do the above is to implement them using class and objects.

In

Leave a Reply

Your email address will not be published. Required fields are marked *