Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

syncronization

0 views
Skip to first unread message

Chunekit Pong

unread,
Jan 30, 2009, 9:00:33 PM1/30/09
to
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();
}
}


Kai-Uwe Bux

unread,
Jan 30, 2009, 9:35:16 PM1/30/09
to
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?

There will be synchronization issues if you have a multiple threads. In that
case, you need to make sure that only one thread accesses the queue at any
time. One typical way is to guard the queue by a mutex.



> I am very messed up in pointers, is the crash related to just simple
> mistake in pointers?

Could be. There is not enough code to say.

This while loop is peculiar. It assumes but does not ensure that the queue
is and stays non-empty. If you call front() on an empty queue, you have
undefined behavior. A core dump could very well be a manifestation of this.

> }


Best

Kai-Uwe Bux

red floyd

unread,
Jan 30, 2009, 11:06:48 PM1/30/09
to

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;
}

Chunekit Pong

unread,
Jan 31, 2009, 1:00:38 AM1/31/09
to

I don't know how to initialize IDs object (a pointer)

do I do somethign like
std::queue< entry_type > *IDs = new queue():

SG

unread,
Jan 31, 2009, 7:47:55 AM1/31/09
to
On 31 Jan., 07:00, Chunekit Pong <worlman...@yahoo.com> wrote:
> I don't know how to initialize IDs object (a pointer)
>
> do I do somethign like
> std::queue< entry_type > *IDs = new queue():

You can't expect people to teach you these basics. Get a book and
learn for yourself. It is important for you to show that you put some
effort into solving your problems. Otherwise you may not get helpful
comments besides "Read a book about C++".

Cheers!
SG

0 new messages