Link to home
Start Free TrialLog in
Avatar of Wanting2LearnMan
Wanting2LearnManFlag for United Kingdom of Great Britain and Northern Ireland

asked on

c++ static callback method question

I have code used to monitor data received over a network.

I have a static instance of a class which is used to calculate the bitrate and has a static method as follows:

static int DataReceivedCallBack(long bytesReceived, void* userData);

Open in new window


This function is called from inside various dlls whenever data is received.

int MyBandwidthManager::DataReceivedCallBack(long dataLength, void* userData  )
{
     MyBandwidthManager* bwMgr = static_cast<MyBandwidthManager*>(userData);
     bwMgr->UpdateBitrate(dataLength);
     
     return 0;
}

void MyBandwidthManager::UpdateBitrate( long bytesReceived)
{		
		unsigned long timeStampNow =  timeGetTime();
                // blah blah
                // do bitrate calculations
 }

Open in new window


The various dlls in question are used to receive video from different sources, I pass the DataReceivedCallBack into these dlls as a callback function.

The problem I am having is that I see sometimes (using debug messages) that timeStampNow contains the exact same time but the function DataReceivedCallBack has been called by two different dlls.

I thought that the fact that the method was static meant that it could be called only once at any one time??? And not twice at the same time??

To calculate the bite rate correctly I need the timestamp between incoming data to be always incrementing.

Any ideas/help??

Thanks
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wanting2LearnMan

ASKER

Thanks guys, good interesting points.

I have added a lock, and I noticed that sometimes the same time was still being returned (due to the speed of the processor and timeGetTime returning milisecs) - so I added a Sleep(1) in for now which should be good enough for my calculations.
 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
seems i was a little bit slow with my suggestions ...

Sara
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial