CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2011
    Posts
    3

    CreateThread and few arguments

    Hey
    I'm stuck with the following code. I need to pass a few variables address to new thread to got a shared access to them but the way I choose seems doesn't work. I got in new thread a good pointer to params class but bool and intereger variable addresses are not proper. Where can be the problem??

    Thanks!

    #include"threads.h"

    class params
    {
    public:
    bool* bl;
    int* i;
    HANDLE* h;

    };


    DWORD WINAPI ThreadProc(
    __in LPVOID lpParameter
    )
    {
    bool* p_b;
    int* p_i;
    HANDLE h;

    params* ptr=(params*)lpParameter;

    p_b=ptr->bl; //that's the first problem (often even 0 value for pointer)
    p_i=ptr->i; //that's the second problem
    h=(*ptr->h);

    return 0;

    }

    void initThread()
    {
    bool a=true;
    int b=1;
    HANDLE h=CreateMutex(NULL,TRUE,L"TEST");


    params prm;
    prm.bl=&a;
    prm.h=&h;
    prm.i=&b;

    CreateThread(0,0,ThreadProc,&prm,0,0);

    }

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: CreateThread and few arguments

    All the variables whose addresses you're passing to the thread function as well as the params object itself are local to the function initThread(). They go out of scope and cease to exist once that function returns. Of course, initThread() does not wait for the newly created thread to finish before returning: That's what threads are made for!

    Please use code tags when posting code.

    Ah, and... Welcome to CodeGuru!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Apr 2010
    Posts
    20

    Re: CreateThread and few arguments

    It's not just about the variables going out of scope, each thread has it's own stack and the params variable he creates is in the main thread. In order to access the variables in the newly created thread it needs to be on the heap or be a global variable.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: CreateThread and few arguments

    Quote Originally Posted by CppCoder2010 View Post
    It's not just about the variables going out of scope, each thread has it's own stack and the params variable he creates is in the main thread. In order to access the variables in the newly created thread it needs to be on the heap or be a global variable.
    Post #2 pointed out that because the mutex and thread handles are local to the function that created the thread and mutex, they will not be available after the function goes out of scope - so it really is a scoping issue.

    Rather than coding in c-style, the OP may want to create a class instead. That way, the thread and mutex handles can be a class member, and the class destructor can ensure the thread is shutdown cleanly and clean up the thread and mutex handles.

    Also, prefer using _beginthreadex over CreateThread.

  5. #5
    Join Date
    Aug 2011
    Posts
    3

    Re: CreateThread and few arguments

    Guys thanks for help, I totally forgot that this variables will be on local stack.....

    Why you prefer _beginthreadex over CreateThread???

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: CreateThread and few arguments

    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured