Thread: Classes and Threads

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    31

    Classes and Threads

    Hey all,

    Since I'm not sure whether to put this in the C++ or Windows section I'm just going to dump it here.

    I have a question regarding classes and threads. Say you have a class which has two static member functions which serve as the entry points for two different threads:

    Code:
    static DWORD WINAPI Thread1(LPVOID pClassInstance);
    static DWORD WINAPI Thread2(LPVOID pClassInstance);
    Now, let's say these two threads are created and started in another member function of the class (which is not static).
    Each thread receives a pointer to the instance of the class that created them so they can use private resources such as member variables and functions.

    My question then goes as follows:
    Imagine both threads reading data from somewhere. This data is manipulated by the same member function. So, what happens if say Thread1 calls this function, gets suspended by the OS halfway through and Thread2 then calls this same function. I mean, is there a potential danger here that they might share data (like Thread2 overwriting something Thread1 did)?

    I believe local variables exist in each of the threads' own address space, but I just want to be absolutely sure! But what about static local variables? Is it then the same memory both threads access?
    Last edited by Halloko; 10-07-2005 at 05:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Initialize concurrent processes in C?
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 12:59 PM
  2. Ok so I now know how to do...
    By PCG33K in forum C++ Programming
    Replies: 15
    Last Post: 10-04-2007, 08:54 AM
  3. C++: Threads and Classes
    By newntrue in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2007, 04:06 AM
  4. classes are pain
    By pizzapie in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2004, 12:55 PM
  5. help with classes or threads or something :)
    By btq in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2002, 11:13 PM