dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Worker thread question

  1. #1
    Join Date
    Apr 2008
    Posts
    93

    Worker thread question

    i had finish the programme (MFC dialog based) but now i have to handover my programme to next batch juniors. i have some questions about the worker thread i am using.

    first question:
    this is what i declare and initialize for my thread function
    Code:
    static UINT ThreadFunction(LPVOID lp)
    why do i have to set the function to Static type and what is (LPVOID lp) different from normal void?

    second question:
    the following code is inside my Threading function
    Code:
    UINT CProjectDlg::ThreadFunction(LPVOID lp)
    {
      CProjectDlg *pPointer;
      pPointer = (CProjectDlg*)lp;
      CClientDC dc(pPointer);
                    
            while(true)
            {
                    pPointer->NormalFunction();
                    Sleep(100);
                    dc.BitBlt(0,0,500,500,(&pPointer->memdcDrawing),0,0,SRCCOPY);
            }
            return true;
    }
    what does the first three lines of codes mean? In my own way of thinking, i think that the code is for linking the two different function type together so they can access each other variables.

    And what does the sleep() really mean when been used in Threading Function?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Worker thread question

    now i have to handover my programme to next batch juniors.
    What is that suppose to mean? That you do the homework for others? Do they gain anything with that, especially when you don't understand your own program?

    why do i have to set the function to Static type
    It's explained here: http://www.codeguru.com/forum/showthread.php?t=343480.

    (LPVOID lp) different from normal void?
    It differs by being a pointer to void, void*. Use F12 to do to the definition of LPVOID and see what it actually is.

    what does the first three lines of codes mean?
    Are you kidding? Who wrote this program? I means ThreadFunction was passed a pointer to a CProjectDlg object, and now you're casting back.

    And what does the sleep() really mean when been used in Threading Function?
    It means the same regardless where it's used. See MSDN for more.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2008
    Posts
    93

    Re: Worker thread question

    Quote Originally Posted by cilu View Post
    What is that suppose to mean? That you do the homework for others? Do they gain anything with that, especially when you don't understand your own program?


    It's explained here: http://www.codeguru.com/forum/showthread.php?t=343480.


    It differs by being a pointer to void, void*. Use F12 to do to the definition of LPVOID and see what it actually is.


    Are you kidding? Who wrote this program? I means ThreadFunction was passed a pointer to a CProjectDlg object, and now you're casting back.


    It means the same regardless where it's used. See MSDN for more.

    first, the juniors will carry on the project and they will do numerous test run using the programme and if there is bugs in the programme, they know where to find the part of the programme that showing the error.

    second, the pointer thing is from one of my school lecturer. he busy prepariong for the next new semester of school, so i cant disturb him. I try to remove the first three lines of code but, when i trying to use variable in normal function, it will show out the error of "name of the variable" undeclared identifier. although i had already declare it at the header file.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Worker thread question

    I think you need a refresher on the basics of C++. A static function exists in a different context than normal class members. It has no implicit "this" pointer. Because of this, you need a pointer back to the class to access any other members. That is why you pass "this" to the thread creation function and you cast it back to the class within the thread function.

  5. #5
    Join Date
    Jun 2004
    Posts
    1,334

    Re: Worker thread question

    Question to Cilu,


    Cilu,


    I was doing a little reading on Threads, I was under the impression that MFC Classes (Dialog Boxes .... etc,) where not thread safe and that you shouldn't be passing refrence to them across thread boundries. So basically you shouldn't be talking to MFC classes in your main app?


    I'm kind of on something else right now and don't have time to test his code, but isn't he referencing/accessing the Dialog Box from the thread a NoNo?
    Rate this post if it helped you.

  6. #6
    Join Date
    Apr 2008
    Posts
    93

    Re: Worker thread question

    Quote Originally Posted by hoxsiew View Post
    I think you need a refresher on the basics of C++. A static function exists in a different context than normal class members. It has no implicit "this" pointer. Because of this, you need a pointer back to the class to access any other members. That is why you pass "this" to the thread creation function and you cast it back to the class within the thread function.
    hmm.. .somehow i can understand a little bit from this explanation.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)