titbits from the world less travelled

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.

posted by admin in c++ and have No Comments

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment