473,729 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::list: remove from front without deleting or looping through whole list

Hello,

I have the following situation:

Thread A is allocating a dataset, doing some low-level calculations
and storing a pointer to the dataset in a std::list via push_back.
Thread B should retrieve the pointer to the first dataset in the list,
remove it from the list, and do some high level analysis.

The problem now is, how do I efficiently retrieve the pointer?

Calling front() and then pop_front() deletes the dataset, i.e. calls
its destructor, so this is not good, since I don't want to copy the
datasets before calling pop_front(), because they are very large...
Calling front() and remove(mylist.f ront()) does the job, but loops
through the whole list and is thus also not very efficient..

Is there any efficient way to retrieve the first object from the list,
and remove the object then from the list, without looping through the
whole list or deleting the object?

If anybody has a good answer to the problem, I would really appreciate
to hear it.

Thanks a lot,
Andy

Jun 8 '07 #1
3 3724
On Jun 8, 12:16 am, Andy <goo...@zogi.de wrote:
>
Thread A is allocating a dataset, doing some low-level calculations
and storing a pointer to the dataset in a std::list via push_back.
Thread B should retrieve the pointer to the first dataset in the list,
remove it from the list, and do some high level analysis.
>From what you describe, you should consider std::deque. For that
matter, measure std::vector and make sure it is not suitable.
>
The problem now is, how do I efficiently retrieve the pointer?

Calling front() and then pop_front() deletes the dataset, i.e. calls
its destructor, so this is not good, since I don't want to copy the
datasets before calling pop_front(), because they are very large...
Calling front() and remove(mylist.f ront()) does the job, but loops
through the whole list and is thus also not very efficient..
Not sure what you mean by "looping through the whole list". For all
you know, your implementation caches std::list::fron t().
Is there any efficient way to retrieve the first object from the list,
and remove the object then from the list, without looping through the
whole list or deleting the object?
By erasing the first element, you only lose the pointer that was
stored in that element. You don't delete the object. I assume you
have another pointer to the same object? If you don't, and you just
erase the first element, you leak memory.

- Anand

Jun 8 '07 #2
On Thu, 07 Jun 2007 22:16:51 -0700 in comp.lang.c++, Andy
<go****@zogi.de wrote,
>Thread A is allocating a dataset, doing some low-level calculations
and storing a pointer to the dataset in a std::list via push_back.
Thread B should retrieve the pointer to the first dataset in the list,
remove it from the list, and do some high level analysis.

The problem now is, how do I efficiently retrieve the pointer?

Calling front() and then pop_front() deletes the dataset, i.e. calls
its destructor,
No, it doesn't, according to what you have so far described.
The list merely stores pointer values. It has no idea about whatever
those pointers might point to and will never call destructors at all.

Jun 8 '07 #3
On Jun 8, 7:16 am, Andy <goo...@zogi.de wrote:
I have the following situation:
Thread A is allocating a dataset, doing some low-level calculations
and storing a pointer to the dataset in a std::list via push_back.
Thread B should retrieve the pointer to the first dataset in the list,
remove it from the list, and do some high level analysis.
The problem now is, how do I efficiently retrieve the pointer?
Calling front() and then pop_front() deletes the dataset, i.e. calls
its destructor, so this is not good,
If the list contains pointers, it doesn't. It just removes the
pointer from the list. (It calls the destructor of the pointer,
but the destructor for a pointer is a no-op.)
since I don't want to copy the
datasets before calling pop_front(), because they are very large...
Calling front() and remove(mylist.f ront()) does the job, but loops
through the whole list and is thus also not very efficient..
Is there any efficient way to retrieve the first object from the list,
and remove the object then from the list, without looping through the
whole list or deleting the object?
If anybody has a good answer to the problem, I would really appreciate
to hear it.
I'm not sure I understand the problem. If you're list contains
pointers, nothing gets deleted by pop_front. If it contains
objects, you copy both on insertion and of removal. (For queues
between threads, I usually use auto_ptr in the interface, so it
is clear who owns what. And most of the time, I find deque more
appropriate than list.)

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #4

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

Similar topics

14
5627
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
11
2270
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to next, if it's ready I take steps to delete it. The thing is that the list is part of a gui application and the system removes the Document* from the list during the execution of the loop body. That means I cant use iterators to iterate over the list....
5
1785
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
3
2141
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is different before and after the sort. If I remove the sorting (and nothing else), both numbers remain the same. int nmerged = 0; do { std::list<mum_pair> mregions;
8
3298
by: emanshu | last post by:
Hi, i am storing data read from some file (text,pdf,gif...) into std::list using push_back function available for list. and my list type is string.( std::list<string> mylist) after storing whole file in list i am trying to writing the data stored in list into new file of same format. if the source file is text then destination new file obtained is correct but if it is pdf then the destination file obtained is not correct, even size is...
44
3870
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be specialized for anything. Thanks, Josh McFarlane
25
3874
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It is important that I iterate through the list backwards, because the items in it have to be processed in reverse order before erasing. However, there does not appear to be an std::list::erase() method defined for reverse iterators.
4
3896
by: TBass | last post by:
Hi, I've got a class that uses a std::list to store tags that need to be written out to a device. I've had no problem with the lists in terms of iterating and reading values. My problem is when I need to pop that value off the list when I'm done with it. In this case, I think the iterator is getting "lost" because I pop off the value it is pointing to at the end of this function. But how do I pop values off one at a time without the...
12
2705
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
0
8921
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
8763
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
9427
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
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
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();...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
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.