URGENT !! QUEUE STL PROBLEM URGENT!!

Discussion in 'C++' started by Sachin Jagtap, Apr 28, 2005.

  1. 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();
    }
    }
    Sachin Jagtap, Apr 28, 2005
    #1
    1. Advertising

  2. 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();
    > }
    > }
    Mark Stijnman, Apr 28, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message news:<>...
    > 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();
    > > }
    > > }
    Sachin Jagtap, Apr 29, 2005
    #3
  4. Sachin Jagtap

    Alvin Guest

    Sachin Jagtap wrote:

    > 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
    Alvin, Apr 29, 2005
    #4
  5. Sachin Jagtap wrote:
    > 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.
    Mark Stijnman, Apr 29, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,039
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    668
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    982
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    534
    Jerry Hill
    Apr 25, 2008
  5. Kris
    Replies:
    0
    Views:
    466
Loading...

Share This Page