LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2012, 10:50 AM   #1
alphisb0t
Member
 
Registered: Jun 2004
Distribution: gentoo 2008.desktop
Posts: 110

Rep: Reputation: 15
reading from and writing to a global linked list properly


Hey guys. I imagine this question is a pretty common one that I thought I could answer but I cannot. Here is my situation:

My program reads data (live) and stores it into my std::list<> which is global in my program. This list grows and within it, there is one other std::list<> which also grows. (the list is a list of structs that itself has one other list as a member)

ie:
struct items{
char data[MAX];
std::list<char*> data;
};

std::list<items> mainlist;

So as I get more live data if there is more data for a particular object, the data list in that object is concatenated. If the data is for a new object then a new list is created and that data is put into that list's data list as a first entry.

My problem is when I want to display this data. In my program I have a while loop that iterates over the global std::list and for each of those it will print out each item in the data list. It seems to work ok, but sometimes strange things happen such as:

showing data:
somedata
somemore data

showing data:
somedata
somemore data

showing data:
someother data

showing data:
somemore data

It seems to randomly be missing information and then later it will show some of the information. I'd expect that for each "showing data" I'd have an increasing amount of ALL information, not just a random set of information.

I'm pretty sure I'm doing something silly, and that there are smarter ways to make this work.

Any help is greatly appreciated!
 
Old 02-07-2012, 11:19 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If your program is multi-threaded, you may be seeing the effect of non-atomic updates to the linked list. If a thread that is updating the list gets suspended while the update is partially complete, other threads will be able to see the incomplete update. If this is the cause, then one common solution is to use code synchronization with semaphores to protect the update code segment, so that reader threads can only access the list after updates are complete.
--- rod.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List with Iterator Corps Programming 3 09-10-2009 12:46 AM
Linked list C exvor Programming 14 06-22-2007 06:06 PM
Linked list manas_sem Programming 3 12-21-2006 01:53 AM
C linked list exvor Programming 4 04-28-2006 05:25 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration