-
April 1st, 2011, 03:43 PM
#1
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?
-
April 1st, 2011, 04:08 PM
#2
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.
-
April 1st, 2011, 04:50 PM
#3
Re: Thread callback handling, best practices?
Originally Posted by Lindley
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.
-
April 1st, 2011, 05:05 PM
#4
Re: Thread callback handling, best practices?
See QueueUserWorkItem api in msdn.
-
April 1st, 2011, 05:19 PM
#5
Re: Thread callback handling, best practices?
Originally Posted by Arjay
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.
-
April 1st, 2011, 07:40 PM
#6
Re: Thread callback handling, best practices?
Originally Posted by Large
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
-
April 2nd, 2011, 03:55 AM
#7
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)
-
April 2nd, 2011, 10:16 AM
#8
Re: Thread callback handling, best practices?
Originally Posted by Large
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.
-
April 2nd, 2011, 12:53 PM
#9
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.
-
April 3rd, 2011, 03:20 AM
#10
Re: Thread callback handling, best practices?
Originally Posted by Large
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|