URGENT !! QUEUE STL PROBLEM URGENT!!

S

Sachin Jagtap

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();
}
}
 
M

Mark Stijnman

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
 
S

Sachin Jagtap

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
 
A

Alvin

Sachin said:
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.
 
M

Mark Stijnman

Sachin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top