Solved

Problem with scope in MFC

Posted on 2000-04-26
4
319 Views
Last Modified: 2012-08-14
I've created a multithreaded program in MFC.  The worker thread currently sits in the background and loops through a socket function (it's a server) that receives records from an etharnet connection and then parses the information and stores it in a class.  The second thread runs on a timer and is the main GUI and updates itself from the information parsed from the records.  My problem is allowing the GUI to access the information received from the socket.  Since the socket thread runs on its own I cannot access that information.  I'm having a serious scope problem and need help.  Thanks for anything you can offer
0
Comment
Question by:xelpmoc2
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 75 total points
ID: 2752304
You need to use global variables or pointers to allow the two threads the opportunity to share the information.  I don't know the exact nature of the data and how it is allocated and managed, so I really say much more than that.

Note that if the two threads will be accessing the data simultaneously you will probably need to synchroize access to the data.  Are you familar with how to do so?  In this case you can probably use a critical section to synchronize access.
0
 
LVL 1

Author Comment

by:xelpmoc2
ID: 2753023
Is it truely as simple as that?  I ask because the person who was helping me on the project informed me that since the thread is running continuously in the background seperate from the rest of the program that accessing the information at will (or off of a timer) would be impossible since the two threads run independantly of each other.  Could using MFC throw a wrench into this also?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2753197
>> Is it truely as simple as that?  
sharing data between multiple threads is usually not that easy 9not THAT hard either).  But the easy part is making it available to both threads (using a global variable, pointer, other mechanism.  the harder part is making it safe to access the data from both threads.  (so the data does not get corrupted)  That will usually require snchronization.  The detaisl of that depend on th eexact nature of the situation.

>>  impossible since the two threads run
>> independantly of each other
That is done al time.

>>Could using MFC throw a wrench into this also
it depends on what you are doing, but probalby not.

Are you familiar with syncronized access in multiple threads.  It is covered in many intermediate/advanced programming texts.   usually it is done using a mutex.
0
 
LVL 1

Author Comment

by:xelpmoc2
ID: 2753231
Thank you nietod, you made my first experience with this web page a good one, it was so nice to get quick and insightful answers.  Thank you so much for your help!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now