473,786 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

URGENT !! QUEUE STL PROBLEM URGENT!!

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(objM essage);
}
thread2
{
if(size of queue is not empty)
{
CMessage objmessage = queue.front(); //SOMETIMES I AM GETTING
EXCEPTION HERE.
queue.pop();
WriteInFile();
}
}
Jul 23 '05 #1
4 2777
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(objM essage);
}
thread2
{
if(size of queue is not empty)
{
CMessage objmessage = queue.front(); //SOMETIMES I AM GETTING
EXCEPTION HERE.
queue.pop();
WriteInFile();
}
}


Jul 23 '05 #2
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.**********@t nw.utwente.nl> wrote in message news:<11******* *************** @g14g2000cwa.go oglegroups.com> ...
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(objM essage);
}
thread2
{
if(size of queue is not empty)
{
CMessage objmessage = queue.front(); //SOMETIMES I AM GETTING
EXCEPTION HERE.
queue.pop();
WriteInFile();
}
}

Jul 23 '05 #3
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
Jul 23 '05 #4

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.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
10813
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
0
1802
by: royal | last post by:
This problem is to simulate an airport landing and takeoff pattern. The airport has 3 runways, runway 1, runway 2 and runway 3. There are 4 landing holding patterns, two for each of the first two runways. Arriving planes will enter one of the holding pattern queues, where the queues are to be as close in size as possible. When a plane enters a holding queue, it is assigned an integer ID number and an integer giving the number of time...
8
6369
by: semedao | last post by:
Hi I have some Queue object that I enqueue other objects into it. I want to check if someobject already exists before enqueue , using the Contains method but italways return false. in the object that I enqueue I implement CompareTo method (Icompareable) but the debugger doesn't go there... thanks
1
1387
by: Thomas Strauss, SRS | last post by:
Hi, I have a really hard time to find the access rights required for monitoring a printing queue or share in windows XP or Win2000/2003 server. We have an application that will remote control another app to print files. To make it more reliable, the printjobs produced are monitored by the application. So the situation is: Application A controls application B. Application B
4
6674
by: rAinDeEr | last post by:
Hi, We have a Test Database in Linux with DB2 v8.2. DB2 was installed in /db2home/db2inst1. Some one moved the all folder db2inst1 to Trash. I moved it back to db2home/db2inst1 But now, when i connect through the command prompt and issue db2 it displays ::
4
4599
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the length of my queue. I started getting compilation errors when I included a length function. <code> template<class ItemType> void Queue<ItemType>::MakeEmpty() {
4
4836
by: lage | last post by:
Hi, I am working with a web site that uses CDONTS from a function in a Com+ component to send mail to clients after sign up etc, as well as sending clients mail to support from a web form. A virtual default SMTP server is running in !!S (6.0) We have moved the system to a new server (Windows 2003 Server as the previous installation) and the mail worked fine a few days. Now we cannot send any mail from the system. SMTP is not my...
1
1359
by: zeuscc | last post by:
I'm doing my final year project which is web-based fax server. I'm using the faxcomexlib library that inside the visual studio 2005 to do the fax function. I had done the send fax part. But now i facing the problem on the receiving fax part. I can know there have incoming queue fax. but after i input the number that i want the information, it become nothing and error come out. Thanks for view the post and giving comments. here's the code...
1
5643
by: Harsh Verma | last post by:
Hi , I am having db2 8.1 fp 12 and yesterday I have installed version 9.5 and since then I am facing an error while issuing any db2 command except db2start and db2stop: DB21015E The Command Line Processor backend process request queue or input queue was not created within the timeout period. while issuing db2level I am getting below error: INGTESTDB:uding604 :/usr/opt/db2_08_01/instance> db2level
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.