C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

URGENT !! QUEUE STL PROBLEM URGENT!!

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Sachin Jagtap
Guest





PostPosted: Thu Apr 28, 2005 7:20 am    Post subject: URGENT !! QUEUE STL PROBLEM URGENT!! Reply with quote



Hi,

I am getting exception while poping item from queue, I am writing
messages coming from client in a queue in one thread and then in other
thread i am reading from queue and writing in file.
I have not implemented any syncronization between reading and
wrinting, just i checking size of queue, if it is not empty i am
reading from queue and wrting to file.

somethign like
class CMessage
{
public:
string strmessage;
};

thread 1
{
CMessage objMessage;
queue.push(objMessage);
}


thread2
{
if(size of queue is not empty)
{
CMessage objmessage = queue.front(); //SOMETIMES I AM GETTING
EXCEPTION HERE.
queue.pop();
WriteInFile();
}
}
Back to top
Mark Stijnman
Guest





PostPosted: Thu Apr 28, 2005 8:33 am    Post subject: Re: URGENT !! QUEUE STL PROBLEM URGENT!! Reply with quote



Whenever one object is manipulated by more than one thread, you should
provide locking around the object. The problem is that focus can shift
from one thread to another at any time. This means there is no
guarantee whatsoever, for instance, that the thread will never switch
somewhere halfway the queue.push command. In multi-threading lingo:
queue.push is not guaranteed to be atomic. Suppose that command is
implemented to first increase the length of the queue, and only after
that assign the new element. If the control passes to the other thread
between those two operations, it may find the queue non-empty, but
without an element assigned. I can't be sure that's what really
happening in your case (I don't know the specifics of the
implementation that you're using), but it's quite likely to be
something similar. Use locks or mutexes to make sure only one thread
accesses an object at the same time (reading at the same time is fine,
but when one thread writes to an object, all other threads *must* wait
until it's done). Good luck,

grtz Mark

Sachin Jagtap wrote:
Quote:
Hi,

I am getting exception while poping item from queue, I am writing
messages coming from client in a queue in one thread and then in
other
thread i am reading from queue and writing in file.
I have not implemented any syncronization between reading and
wrinting, just i checking size of queue, if it is not empty i am
reading from queue and wrting to file.

somethign like
class CMessage
{
public:
string strmessage;
};

thread 1
{
CMessage objMessage;
queue.push(objMessage);
}


thread2
{
if(size of queue is not empty)
{
CMessage objmessage = queue.front(); //SOMETIMES I AM GETTING
EXCEPTION HERE.
queue.pop();
WriteInFile();
}
}


Back to top
Sachin Jagtap
Guest





PostPosted: Fri Apr 29, 2005 8:04 am    Post subject: Re: URGENT !! QUEUE STL PROBLEM URGENT!! Reply with quote



Hi Mark,

Thanks for your reply

I am having only one reader and one writer thread, do I still need to
use mutex? If not what could be the possible reason for crash.

But with one reader and one writer how can I synchronize this. As
reader and writer are two diff threads and there is no common function
between them.


Thanks!!

regards,
Sachin


"Mark Stijnman" <m.a.stijnman (AT) tnw (DOT) utwente.nl> wrote

Quote:
Whenever one object is manipulated by more than one thread, you should
provide locking around the object. The problem is that focus can shift
from one thread to another at any time. This means there is no
guarantee whatsoever, for instance, that the thread will never switch
somewhere halfway the queue.push command. In multi-threading lingo:
queue.push is not guaranteed to be atomic. Suppose that command is
implemented to first increase the length of the queue, and only after
that assign the new element. If the control passes to the other thread
between those two operations, it may find the queue non-empty, but
without an element assigned. I can't be sure that's what really
happening in your case (I don't know the specifics of the
implementation that you're using), but it's quite likely to be
something similar. Use locks or mutexes to make sure only one thread
accesses an object at the same time (reading at the same time is fine,
but when one thread writes to an object, all other threads *must* wait
until it's done). Good luck,

grtz Mark

Sachin Jagtap wrote:
Hi,

I am getting exception while poping item from queue, I am writing
messages coming from client in a queue in one thread and then in
other
thread i am reading from queue and writing in file.
I have not implemented any syncronization between reading and
wrinting, just i checking size of queue, if it is not empty i am
reading from queue and wrting to file.

somethign like
class CMessage
{
public:
string strmessage;
};

thread 1
{
CMessage objMessage;
queue.push(objMessage);
}


thread2
{
if(size of queue is not empty)
{
CMessage objmessage = queue.front(); //SOMETIMES I AM GETTING
EXCEPTION HERE.
queue.pop();
WriteInFile();
}
}

Back to top
Alvin
Guest





PostPosted: Fri Apr 29, 2005 11:58 am    Post subject: Re: URGENT !! QUEUE STL PROBLEM URGENT!! Reply with quote

Sachin Jagtap wrote:

Quote:
Hi Mark,

Thanks for your reply

I am having only one reader and one writer thread, do I still need to
use mutex? If not what could be the possible reason for crash.

But with one reader and one writer how can I synchronize this. As
reader and writer are two diff threads and there is no common function
between them.


Thanks!!

regards,
Sachin

Use a mutex to synchronise them. The writer locks the mutes, do the writing,
then unlocks the mutex. If the reader locks the mutex, reads, then unlocks
the mutex.

The synchronisation occurs when one attempts to lock the mutex while the
other had it already locked. For exmaple, if the reader tries to lock the
mutex after the writer had already locked it, then the reader will wait for
the mutex to be unlock (i.e. when the writer unlocks the mutex).

This examples is just a simplification, I suggest reading up on
Multi-Threaded Programming to get the full picture.

--
Alvin

Back to top
Mark Stijnman
Guest





PostPosted: Fri Apr 29, 2005 12:08 pm    Post subject: Re: URGENT !! QUEUE STL PROBLEM URGENT!! Reply with quote


Sachin Jagtap wrote:
Quote:
Hi Mark,

Thanks for your reply

I am having only one reader and one writer thread, do I still need to
use mutex? If not what could be the possible reason for crash.

But with one reader and one writer how can I synchronize this. As
reader and writer are two diff threads and there is no common
function
between them.


Thanks!!

regards,
Sachin



It doesn't matter that the threads don't share any functions, they
share an -object- and its -data-. They both operate on the same set of
data, in this case a queue. Adding an element to the queue will change
the data. Actually, now that I think of it, the other thread also
changes the data when popping one element from the queue. So both
threads can change the data embedded in the queue object, and there is
no guarantee that these operations will not be interrupted by switching
threads - remember, the OS can switch the active thread whenever it
wants. So if one thread is halfway pushing, then the active thread
switches to the other, and then that thread pops an element, the queue
object can easily be left in an undefined state. Or the other way
around: halfway popping an object, the other thread might try to push a
new one into the queue, leaving again
undefined state of the queue. So yes, you have to add mutexes wherever
you access a shared object. It *should* help - whenever you have a
program that works single-threaded and suddenly stops working when you
add an extra thread, it's almost always due to shared data being
accessed in a wrong way.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.