Can we use CriticalSection synchronization mechanism without using threads?

  • Thread starter Thread starter bhavya internship
  • Start date Start date
B

bhavya internship

Guest
Hi All,

My understanding about CriticalSection is - Using Critical Section one thread will lock the resource (area of code). Once the thread has performed it's action, it releases the resource so that other thread can lock the resource.

But in the below scenario, By calling one function from other functions, without using the threads we are using critical section and locking the resource.

As a beginner, i am wondering and wanted to know that, Can we use synchronization techniques to lock the resources without using the threads?

And here in my scenario when the application is launching we are calling the function which has CriticalSection. And also from OnTimer() function for each second we are calling the Criticalsection function.

Below is the code snippet:

In EventGenerator.cpp file

From the below ::InitUPS() function, we are calling ::CreateUPSControl() which has implemented with Critical Section.
InitUPS() function is calling when launching the application.

ErrCode CEventGenerator::InitUPS()
{
//This function should only be called once on startup
bool bHadUPS = (theUPSControl != NULL);
ErrCode err = ERR_SUCCESS;

if (bHadUPS == false)
{
err = CreateUPSControl();
}

return(err);
}

And from below function also we are calling ::CreateUPSControl() function.

GetUPSEvents() function is calling from the OnTimer() function

ErrCode CEventGenerator::GetUPSEvents (CEvents * anEventsPtr)
{
DWORD bRequiredUsages;
bool bHadUPS = (theUPSControl != NULL);
bool bHaveUPSNow = false;
ErrCode err = ERR_SUCCESS;
long bHadAC = 1;

assert(anEventsPtr != NULL);

if (theUPSControl == NULL)
{
err = CreateUPSControl();
}

if (err == ERR_SUCCESS)
{

err = theCurrentData.GetFrom(theUPSControl);

if (err == ERR_SUCCESS)
{
theIsCurrentDataFilled = true;
bHaveUPSNow = true;
}
else
{
EnterCriticalSection(&theControlCritSect);
delete theUPSControl;
theUPSControl = NULL;
LeaveCriticalSection(&theControlCritSect);

if (thePreviousData.theACPresent)
{
bHadAC = *thePreviousData.theACPresent;
}

thePreviousData.ClearUp();
theCurrentData.ClearUp();
}
}


EnterCriticalSection(&theDataCritSect);

theHaveWeGotComms = (err == ERR_SUCCESS);

// Only bother updating theSafeData if we've got comms (if not, data is probably garbage anyhow)
if (theHaveWeGotComms)
{
if (theIsCurrentDataFilled == true)
{
theSafeData = theCurrentData;
}
else
{
theSafeData = thePreviousData;
}
}
else
{
theSafeData.ClearUp();
}

LeaveCriticalSection(&theDataCritSect);

*anEventsPtr = theEvents;
theEvents.ClearAllEvents();

return err;
}

Below is the function where we are using CriticalSection.

ErrCode CEventGenerator::CreateUPSControl()
{
CUPSControl * ctl = NULL;
long vendorID = APCVENDORID;

ErrCode retErr = CUPSControl::CreateNewUPSControl(&ctl, 0, vendorID);

std::auto_ptr<CUPSControl> destroyer(ctl);

if (retErr == ERR_SUCCESS)
{
assert(ctl != NULL);

retErr = thePreviousData.GetFrom(ctl);

::EnterCriticalSection(&theDataCritSect);
theSafeData = thePreviousData;
::LeaveCriticalSection(&theDataCritSect);

if (retErr == ERR_SUCCESS)
{
CheckForInitialEvents(thePreviousData);
::EnterCriticalSection(&theControlCritSect);
delete theUPSControl;
theUPSControl = destroyer.release();
::LeaveCriticalSection(&theControlCritSect);
}
else
{
thePreviousData.ClearUp();
theCurrentData.ClearUp();
}
}

return retErr;
}

Continue reading...
 
Back
Top