Re: syncronization
Chunekit Pong wrote:
when when I run the VirtualScreen.cpp code, it will crash like this
(screen shot captured)
http://www.oniva.com/upload/1356/crash.jpg
it is something related to pointers which i am no familiar with.
Basically,
1) RFIDSock Class - i defined a key value pair - ID / timestamp, then
I insert the key value pair into a queue.
2) VirtualScreen Class - I have a reference to RFIDSock object
(pointer) and I am accessing RFIDSock's "queue" object (*IDs) and pop
any object the timestamp is old enough.
since both RFIDSock / VirtualScreen Class - both function is keep
calling every seconds or miliseconds.
RFIDSock - is inserting to the queue
VirtualScreen - is removing element from the queue
will there be some syncrohization problem for both function accessing
the queue every mililseconds / seconds?
I am very messed up in pointers, is the crash related to just simple
mistake in pointers?
=====================
RFIDSock.h
=====================
#include <map>
#include <list>
#include <string>
#include <queue>
#include "VirtualScreen.h"
typedef const char* id_type;
typedef unsigned int timestamp_type;
typedef std::pair<id_type, timestamp_type> entry_type;
//class CVirtualScreen;
class CVirtualScreen;
class RFIDSock
{
public:
std::queue< entry_type > *IDs;
};
=====================
RFIDSock.cpp
=====================
#include "RFIDSock.h"
#include <queue>
#include <time.h>
class CVirtualScreen;
//--------------------------------------------------------------
void RFIDSock::OnDataReceived(const LPBYTE lpBuffer, DWORD dwCount)
{
entry_type entry;
entry.first = (const char*)m_bReceiveBuffer;
entry.second = seconds;
IDs->push( entry );
}
=====================
VirtualScreen.h
=====================
RFIDSock *rfidsock;
=====================
VirtualScreen.cpp
=====================
void VirtualScreen::ProcessMessages()
{
unsigned int current = time (NULL);
while ( current - rfidsock->IDs->front().second > duration){
rfidsock->IDs->pop();
}
}
What Kai-Uwe said, plus the obvious. ARe you initializing IDs properly
in the constructor? Why are you using a pointer to a queue?
To do what Kai-Uwe suggests, rewrite the loop:
while (!rfidsock->IDs->empty())
{
if (current - rfidsock->IDs->front().second > duration)
rfidsock->IDs->pop();
else
break;
}