Solved

Accessing  data from two threads

Posted on 2004-09-13
6
189 Views
Last Modified: 2013-11-20
Hello,

I am writing an application which uses two threads.
I have a list<int> variable in my Document class.
THREAD 1
In this thread, I am filling a list from back i.e pDoc->list.insert(list.begin(),data);
THREAD 2
In this thread, I am extracting each element from top of the list, and displaying it on the UserInterface i.e
data = pDoc->list.pop_front();
Display(data);

THREAD1 cotinously reads and adds data at the bottom of the list while THREAD 2 gets  data from the top of the list and displays it.

Can I directly access the list memeber available in my document class as above in the two threads.
When I tried the above, it worked fine when the list had limited number say 10 to 30 records, but as the size of list was growing to say 10,000 records the program was crashing.

Can anyone please help me to solve this ?

Thanks
Madhavi
0
Comment
Question by:j_madhavi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 30 total points
ID: 12052897
list::push_back
Adds an element to the end of a list.

list::pop_front
Deletes the element at the beginning of a list.

Plz refer to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrflist_members.asp

As you are using threads, it is advisable you synchronize between these 2 threads. If the first thread is adding element #10 in the list and the same is being accessed by the second thread, this may lead to crash.You can refer to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_multithreading.3a_.how_to_use_the_synchronization_classes.asp
0
 
LVL 1

Expert Comment

by:Ilango
ID: 12071822
Fine.. To be specific have a critical section included in your document object and lock it before you make a insertion and release once you are done. The same should be done for reading also. From your explanation, I can say you need a single Critical session and not seperate read and write locks.

If needed use the thread safe InterlockIncrement/InterlockedDecrement for any index based operation.

Thread synchronization is closely associated with the implementation requirement and you have to decide on how better your data will be manipulated.

Cheers !
0
 

Expert Comment

by:l3st4rd
ID: 12091571
to give an example:
first declare a critical section object in your header file:

CRITICAL_SECTION csListAccess;

now initialize the critical section BEFORE you start the threads:

    ::InitializeCriticalSection(&csListAccess);

the new thread code will look like this:
THREAD1:
    ::EnterCriticalSection(&csListAccess);
    pDoc->list.insert(list.begin(),data);
    ::LeaveCriticalSection(&csListAccess);

THREAD2:
    ::EnterCriticalSection(&csListAccess);
    data = pDoc->list.pop_front();
    ::LeaveCriticalSection(&csListAccess);

and don't forget to delete the critical section again (maybe in the destructor):
    ::DeleteCriticalSection(&csListAccess);

of course u need to place i.e. a pointer to the critical section into the thread parameter so that u'll get access to the critical section from within the thread functions.
0
 

Expert Comment

by:curero
ID: 12483157
A simpler (but not the finest) MFC solution:

    CMutex mtxList;

 (you can make it global for easy access)

 for each access to the list:
   mtxList.Lock();
   //access the list
   mtxList.Unlock

 When the application is closed, the kernel will clean it (no need to destroy it). You can add a time-out (just to be sure) in the 'lock' call.
 
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 134
repeatSeparator  java  challenge 13 58
Whole sheet autoscrub still needed 19 53
Prevent this page from creating additional dialogs. 3 715
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question