Solved

Visual C++ why does seperate thread not regonize globals/defines.

Posted on 2012-04-02
7
439 Views
Last Modified: 2012-04-02
I have an application written in C++ using Visual C++ 2009. In this application I have a seperate 'watcher' thread that listens for messages targeted for this seperate thread. In my application I have a header file that let's call common.h. In this common.h file I have defined some globals and #defines that I want the thread to reconize. But when I run the application and put a break point inside of the seperate thread the globals or defines are not set to a value that I inialized them to. Will a seperate thread, even though in the same application, not reconize globals/defines, from an included file?

-Thanks
0
Comment
Question by:atomicgs12
  • 3
  • 2
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 37797123
You currently haven't given enough information to answer your question. You'll have to provides some specific code.

One guess : are your globals defined as static ?
0
 

Author Comment

by:atomicgs12
ID: 37797190
no globals defined as static more like: #define SKIP_THIS_CODE 1
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 37797215
As I said - that was a guess. I can't do anything more than guess, unless you provide actual code.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 37797419
if you need shared göobal variables you normally would use the volatile keyword in order to prevent the compiler from optimizing access.

if the thread code is in different cpp file than the code which sets the initial values to the shared variables, you may define the variable only in one (main) cpp and use extern keyword in other (thread) cpp file(s). otherwise the main thread and the worker thread would use their own instance of the variable but would not share.

an alternative to global variables is to use static class members.

Sara
0
 

Author Comment

by:atomicgs12
ID: 37797554
The thread code is in the same cpp file. The #define is in an .h file that is included at the beginning of the cpp file, i.e. #include "Common.h". In Common.h the define is defined as
' #define SKIP_THIS_CODE 1'. SKIP_THIS_CODE is used in the watcher thread as:

#ifdef SKIP_THIS_CODE
     the code I want to run
#else
     unwanted code
#endif

in the case above the 'unwanted code' is always run. Can I NOT use the #define SKIP_THIS_CODE in the thread?

Thanks
0
 

Author Closing Comment

by:atomicgs12
ID: 37797609
Found a slight programming error on my part but your suggestion did point me in the correct direction to get it working. Thanks
0
 
LVL 33

Expert Comment

by:sarabande
ID: 37799539
the #ifdef preprocessor means is an abbreviation of #if defined. that means regardless whether you define the macro as 1 or 0 it would be defined, means the condition is true, granted that the include statement of the header file where it is defined is above the #ifdef. the only way to make that reverse is to use #undef .

Sara
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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

8 Experts available now in Live!

Get 1:1 Help Now