CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2000
    Location
    Langesund, Telemark in Norway!
    Posts
    292

    Thread callback handling, best practices?

    Hi

    I have a worker-thread that send a callback WM_APP-message to the parent when finished.

    (.... workerthread end...)
    ::SendMessage(hParent, WM_APP+101, NULL, NULL);
    return 0;
    }

    In the parents messageloop we handle data from worker thread and want to create a new worker thread todo next job:

    (.... messageloop in mainprogram ....)
    if{message == WM_APP+101)
    {
    DWORD dwCode;
    m_thread.Stop(dwCode); //Deadlock since CThread syncronize?
    }

    I'm currently using the CThread wrapper: http://www.codeproject.com/KB/threads/cthread.aspx

    There much be a common practice to manage such scenario. What would you do, with or withour the CThread implementation?
    ==========================
    Lars Werner aka Large
    http://lars.werner.no/
    ==========================

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Thread callback handling, best practices?

    If you are waiting for the completion of one thread to start another one, then you might be looking for the thread pool pattern, in which you have a persistent background thread (or threads) and dispatch tasks to it to be processed. This saves you the thread teardown/setup time between each task.

    I believe WinAPI has some kind of thread pool interface available, but I'm not familiar with it.

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

    Re: Thread callback handling, best practices?

    Quote Originally Posted by Lindley View Post
    I believe WinAPI has some kind of thread pool interface available, but I'm not familiar with it.
    From what I've read I remember that QueueUserAPC() is an essential Win API function for that purpose. I have never used that myself though, so I can't really tell you more about it.
    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.

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

    Re: Thread callback handling, best practices?

    See QueueUserWorkItem api in msdn.

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

    Red face Re: Thread callback handling, best practices?

    Quote Originally Posted by Arjay View Post
    See QueueUserWorkItem api in msdn.
    Oops! Mixed them up...
    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.

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Thread callback handling, best practices?

    Quote Originally Posted by Large View Post
    Code:
    (.... workerthread end...)
    ::SendMessage(hParent, WM_APP+101, NULL, NULL);
    return 0;
    }
    Try ::PostMessage() instead of ::SendMessage(). SendMessage() is waiting for hParent to send back an LRESULT response, and there is thus a potential for deadlock since hParent kills the thread. PostMessage() doesn't wait; it simply places the message in the message queue for hParent and then continues execution with the next statement (here, return 0;).

    Mike

  7. #7
    Join Date
    May 2000
    Location
    Langesund, Telemark in Norway!
    Posts
    292

    Re: Thread callback handling, best practices?

    MikeAThon:
    That was it...
    I mixed SendMessage/PostMessage from earlier code.

    By adding some criticalsections everything synched great for getting text between threads and main program.

    All:
    As for the threadpool way todo it; how much do you gain to avoid setup/teardown of threads? (The thread is simple fileoperations)
    ==========================
    Lars Werner aka Large
    http://lars.werner.no/
    ==========================

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

    Re: Thread callback handling, best practices?

    Quote Originally Posted by Large View Post
    As for the threadpool way todo it; how much do you gain to avoid setup/teardown of threads? (The thread is simple fileoperations)
    With QueueUserWorkItem, you make one api call and it handles all the thread pooling for you. That means, creating and destroying threads, cleaning up handles, etc.

  9. #9
    Join Date
    May 2000
    Location
    Langesund, Telemark in Norway!
    Posts
    292

    Re: Thread callback handling, best practices?

    Arjay:
    Sure, if I made manage code I'll use QueueUserWorkItem()

    For info, I found a great article for the new Threadpool in Vista: http://msdn.microsoft.com/en-us/magazine/cc163327.aspx

    I cannot used that case the work is spec-ed to work on WinXP, Vista and W7.
    ==========================
    Lars Werner aka Large
    http://lars.werner.no/
    ==========================

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

    Re: Thread callback handling, best practices?

    Quote Originally Posted by Large View Post
    Arjay:
    Sure, if I made manage code I'll use QueueUserWorkItem()
    Sure there is a managed ThreadPool.QueueUserWorkItem, but I'm referring to the native C++ api.

    http://msdn.microsoft.com/en-us/libr...57(VS.85).aspx

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