Thread: Variable goes out of scope in the child thread when parent thread returns

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    Variable goes out of scope in the child thread when parent thread returns

    ok guys,
    a quick newbie question:

    how do i pass a variable into a thread when i still need that variable AFTER the parent thread returns. after hours of experimenting, i found the problem is when the parent thread returns, the variable in the child thread (formally passed by the parent thread) goes out of scope and disappears....

    A few ideas i thought of

    1) use static ?? i am now too sure on this

    2) use a gobal variable?? i think it still goes out of scope too
    anyway this is not very elegant method

    3) anything???


    in summary:
    - a user interface thread starts a worker thread
    - user interface thread returns so that the program doesnt freeze
    - worker thread still needs to work around with the variable
    <-- problem with the retaining of the variable when user interface thread goes out of scope


    using
    - visual c++ 6.0
    - win98SE
    - API, dont know much about MFC till i learn more on the basic APIs that make up MFC

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Use new to dynamically allocate the variable. delete it when you are done.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    114
    thnx for your valuable suggestion.
    This is what i did:

    Code:
    	char *thread_buffer;
    	thread_buffer = new char[];
    	thread_buffer = (*(start_thread_parm*)thread_parm).comd;  // dynamically allocating the variable as suggested
    	SetEvent((*(start_thread_parm*)thread_parm).continue_broadcast_handle); // tells the parent thread that i have finished copying the parmeter to the child thread and it's ok to continue to return
    This is the class of "start_thread_parm" summarized
    Code:
    class start_thread_parm{
    public:
    {
    char *comd;
    HANDLE continue_broadcast_handle;
    }

    is this the way to do it?
    i keep getting the same problem when the parent thread returns, ie. the variable goes out of scope when the parent thread returns.
    thread_buffer becomes empty after the line "SetEvent((*(start_thread_parm*)thread_parm).conti nue_broadcast_handle); "

    I am doing something wrong here. any help plz?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data from Child to Parent
    By queen_ayeka in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2007, 04:39 PM
  2. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Creating Child with parent of Child
    By SilkySmooth in forum Windows Programming
    Replies: 3
    Last Post: 06-28-2003, 12:15 PM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM