Thread: CreateThread and lpParam

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    CreateThread and lpParam

    Hi, I was trying to create a thread from an object member function, and trying to pass 'this' to the CreateThread() function in the thread parameter function. In the thread procedure, I did:
    Code:
    (Class)& myRef = *(Class*)lpParam;
    However, it didn't work - I ended up with the wrong object, filled with garbage. Then, I tried doing the pointer-pointer thing:
    Code:
    Class* temp = this;
    CreateThread(..., (LPVOID)&temp, ...);
    //in thread procedure:
    (Class)& myRef = **(Class**)lpParam;
    This time it worked. Does anybody know why I needed to use a pointer-pointer? MSDN says the parameter should be "a pointer to a variable to be passed to the thread". Does CreateThread dereference the parameter, make a copy of it and pass the pointer of the copy to the thread, or what?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Does anybody know why I needed to use a pointer-pointer? <<

    You don't. That parameter in CreateThread is of type void* so just cast this (which is already a pointer to object 'Class') in your call to CreateThread.
    Code:
    CreateThread(..., (LPVOID)this, ...);
    Now all you need do in your threadproc is cast to a pointer of the original c++ class type and use that, ie
    Code:
    //in threadproc
    Class *pClass=(Class*)lParam; //or use reinterpret_cast
    Your original pointer-to-a-pointer requirement resulted from passing the address of a pointer (&this) as the lpParameter parameter of CreateThread.
    Last edited by Ken Fitlike; 06-02-2004 at 10:45 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Your original pointer-to-a-pointer requirement resulted from passing the address of a pointer (&this) as the lpParameter parameter of CreateThread.
    Nonono, I tried what you suggested at first (that was my first thought), then when that failed I tried the pointer-pointer. Unless, of course, I made a typo somewhere and my half hour of double-checking failed to turn it up... I guess that's possible still Maybe I'll do a test later.

    Besides, when I tried doing pointer-pointer, the sole reason I created a separate pointer variable to hold this was because (&this) wouldn't compile (and it shouldn't - this isn't a variable with an address, as far as I know).
    Last edited by Hunter2; 06-02-2004 at 11:32 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "this" has type "pointer to your class" and using address-of operator (&) on it isn't a problem.

    Have a look at the code in this thread. It demonstrates the same concept that Ken was showing.

    gg

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    "this" has type "pointer to your class" and using address-of operator (&) on it isn't a problem.
    I'm not a total newbie But I do know that MSVC gave me a compile error on:
    (LPVOID)&this
    &(LPVOID)this
    (LPVOID)(&this)
    etc.

    Maybe a glitch in MSVC, though I can maybe understand why an error would be generated (something along the lines of trying to pass &(456) ).

    Perhaps I was wrong about the cause of the problem, although then I don't know what might have actually been the problem (it worked after just adding the extra pointer layer, then as I remember I switched it back and it stopped working again, etc.). Will try running a test now... brb

    **EDIT**
    Just ran a test in a new project. You're right It does work. *scratches head* Now I wonder what went wrong before... Although I still get a compile error if I try &this.
    Last edited by Hunter2; 06-02-2004 at 04:20 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yeah, my bad......the address-of operator only works on an l-value and "this" is an r-value only.

    So you created a temp l-value on the stack and passed in the address of the stack variable. The only problem with that is you have to ensure that the thread executes "(Class)& myRef = **(Class**)lpParam;" before the function containing the stack variable returns (which will clean up the stack).

    Or just use "this", straight up.
    But don't you want to just use the MfnThread class instead?

    (Pronounced "M"-"F'n"-"Thread"....)

    gg

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The only problem with that is you have to ensure that the thread executes "(Class)& myRef = **(Class**)lpParam;" before the function containing the stack variable returns (which will clean up the stack).
    Yeah, I realized that shortly after my second-last post But then, I'm scrapping that whole project and starting over anyways (it doesn't work, needs a total structure re-working, I can't figure out which set of code is the most recent and likely none is, etc.), so w/e
    But don't you want to just use the MfnThread class instead?
    No. I want to make my own, called Thread, right after I finish rewriting my socket wrapper, which will be after I finish this comp sci project (hopefully today or tomorrow), and then after I finish exams, and then after I decide to get off my butt and do it (hopefully within a couple years)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed