Global or not Global

This is a discussion on Global or not Global within the C Programming forums, part of the General Programming Boards category; Hi Guys This is my first post on this forum. I am trying to get save some information to be ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    34

    Global or not Global

    Hi Guys
    This is my first post on this forum. I am trying to get save some information to be used later on. I have two threads (program/processes) running at the same time. I need to get a variable A value to save in variable B which i can use in my second thread later on. for example

    In Thread A

    B = A;

    In Thread B

    something = B;

    Both thread A and B are independent to each other. My problem is how to share this information between these two threads. I have tried to use global variable but it didn't work. I defined variable B as global in one of the threads and then redefine the same variable with "extern" keyword in one of the header files which is common to both threads. With some testing i could see the value assigned to B in thread A(by printf) but then in thread B when i do a printf on B variable it is printed as 0. Can u guys help me out in this. I appreciate your time and consideration in this regard.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    It sounds to me like your trying to share information between processes. You seem to be under the impression that this is how multi-threading is done.
    What are you really doing? How are you creating these "threads"? What operating system? Can you post some code?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by iMalc View Post
    It sounds to me like your trying to share information between processes. You seem to be under the impression that this is how multi-threading is done.
    What are you really doing? How are you creating these "threads"? What operating system? Can you post some code?
    Hi iMalc
    All i am trying to do is to save variable 'A' value in thread 'A' in Variable 'B' so that i can use this value later on anywhere i want to use. As long as the code is concerned, i am just trying to test this in already written code (which is quite big) and i am not having success in it. I am running this multithreading code in XP.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are there DLL's involved in this [and by that I don't mean "system" DLLs such as the MSVC runtime DLL's, but DLL's produced by the source from your project] - if so, you can't trivially share global variables between a DLL and the main executable. This may be the source of your problems, but without more details, it's hard to say.

    There is no reason why global variables can't be used between threads as such - although you may need to make sure that the compiler doesn't optimize the access to your variables into registers - so a volatile keyword could be necessary too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by matsp View Post
    Are there DLL's involved in this [and by that I don't mean "system" DLLs such as the MSVC runtime DLL's, but DLL's produced by the source from your project] - if so, you can't trivially share global variables between a DLL and the main executable. This may be the source of your problems, but without more details, it's hard to say.

    There is no reason why global variables can't be used between threads as such - although you may need to make sure that the compiler doesn't optimize the access to your variables into registers - so a volatile keyword could be necessary too.

    --
    Mats
    Yes there are the DLL involved in the whole source code. So again in this situation how can i accomplish what i am trying to do.
    Thanks

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    Perhaps you should post some code showing the problem?
    It's difficult to understand the problem. Also, it's also proffered to not to use global variables, but passing a pointer to your thread instead. You may have to use locking to avoid problems too, though.
    But again, show a little snippet of what you're doing and maybe we can analyze it as see what needs to be done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by c_geek View Post
    Yes there are the DLL involved in the whole source code. So again in this situation how can i accomplish what i am trying to do.
    Thanks
    The easiest way to share something between two units like that (an .EXE and a .DLL) is to have a function call that you perform to hand over the data from one side to the other.

    Another option is to use the parameter(s) of the thread creation to pass a data structure which has amongst it's content the address of some data to be shared.

    Or you can set up various named entities, such as named semaphores, named pipes or named shared memory, that each thread will "open" or "create" as necessary.

    Which is the best choice for your depends a lot on what the ACTUAL problem you are trying to solve is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 11:02 AM
  3. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  4. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  5. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21