Thread: Thread Memory

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Thread Memory

    If I create a thread using CreateThread( ), does it share the same memory for global variables as the main thread? So if I changed a global variable from the main thread, would the value be changed in the new thread?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Yes. If you have a global variable the threads can use it. This is my experience with it. Back in the day when i didnt have a clue how to pass a chunk of data as a void pointer i used a bunch of global variables to control my poorly writen threads =P

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    changed a global variable from the main thread, would the value be changed in the new thread?
    <<<

    On a single processor system, yes.

    Not necessarily so in a multiprocessor - the main and worker threads will each load the original value into their processors cache, in a poorly designed application, it is possible for both threads to then modify the value in their own processors cache the final value being written back to the programs main memory depending on whichever thread happened to flush it's cache last.

    *** EDIT ***

    As an aside...

    >>> CreateThread( )

    ... has been the topic of much debate. It is now claimed by MS that the call is safe, however, for many years, it has been implicated in memory leaks. I and everyone I know, always use _beginthread() or _beginthreadex() to spin threads.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Right. It can 'see' global data just as well as other parts of your program. HOWEVER, you must be extremely careful. What if one part of the program is changing the contents of that data just as the thread get's it's turn? Read about locks, mutexes, semaphores, and critical sections before proceeding!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ... has been the topic of much debate. It is now claimed by MS that the call is safe, however, for many years, it has been implicated in memory leaks. I and everyone I know, always use _beginthread() or _beginthreadex() to spin threads.
    That's an important point. If you are using *any* of the C-runtime functions (and who doesn't??), CreateThread() WILL cause small memory leaks (unless it has indeed been fixed - which I honestly doubt, BTW).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Alright, thanks. I'll definitely re-think how I'm using my threads in that case, and I'll look into _beginthread( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    If you use _beginthread don't forget to add "/MT" to your command line (in project options in MSVC++, set for multithreaded), otherwise you'll come back here asking us why you get an "undeclared identifier" for _beginthread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  5. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM