Click here to Skip to main content
16,016,501 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: switch on multi threading in visual studio using boost libraries Pin
Stuart Dootson10-Jul-09 4:46
professionalStuart Dootson10-Jul-09 4:46 
GeneralRe: switch on multi threading in visual studio using boost libraries Pin
minkowski10-Jul-09 4:53
minkowski10-Jul-09 4:53 
GeneralRe: switch on multi threading in visual studio using boost libraries Pin
Stuart Dootson10-Jul-09 5:06
professionalStuart Dootson10-Jul-09 5:06 
Questionlinking a CiSliderX with a spin edit buddy pair (working with discrete values) [modified] Pin
swati10109-Jul-09 20:28
swati10109-Jul-09 20:28 
QuestionHelp with threads Pin
FISH7869-Jul-09 16:54
FISH7869-Jul-09 16:54 
QuestionRe: Help with threads Pin
«_Superman_»9-Jul-09 19:44
professional«_Superman_»9-Jul-09 19:44 
AnswerRe: Help with threads Pin
FISH78610-Jul-09 1:51
FISH78610-Jul-09 1:51 
AnswerRe: Help with threads Pin
Roger Stoltz9-Jul-09 22:23
Roger Stoltz9-Jul-09 22:23 
Phweeew...

To be honest, there's a lot of things wrong with your code and how you think you should go about multithreading.
Let's try and pin-point a few.

  • Use AfxBeginThread() when building with MFC framework
    This must be done to keep the framework happy. Otherwise you'll get into obscure problems that are hard to track down.
  • Do NOT touch the GUI from a secondary thread
    This is very important, especially in MFC since e.g. SetDlgItemText() sends a message to the windows of the control. The difference between sending and posting a message is that sending will block until the message has been processed by the receiving thread. This means that if the thread that "owns" the window doesn't process messages, you'll get a nice little deadlock.
  • Worker threads doesn't process messages
    Your calls to ProcessMessages() are, apart from completely unnecessary, fundamentally wrong. No messages will be sent to your threads. You need to differentiate between so called worker threads that doesn't process messsages and UI-threads.
  • Your main thread doesn't process messages
    After you have called ProcessMessages(), your main thread doesn't process messages any longer. This means that your secondary threads will hang when calling SetDlgItemText() since the receiving window/thread will not consume the message. One way to solve it would be to use ::MsgWaitForMultipleObjects() while waiting for the threads to complete. The correct way would be to return from the function that creates the threads and revert to the window procedure for the main thread. You get into this kind of trouble since you're trying to learn multithreading, but you're using it in a way that contradicts with the reason for having multiple threads since you're still doing things sequentially.

I suspect that the reason why your thread #2 is "still running" when you get the message box is a combination of the above. In other words: your thread #2 hangs because of the call to ::Sleep() since by the time it resumes execution your main thread has stopped processing messages. You should really have a look at the return value from ::WaitForMultipleObjects().

Some words about what you should do.... Shucks | :-\
Forget about the article you referred to and start here[^]. This is, in my opinion (and a lot of others), the absolute best article for starting out doing multithreading.


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown


GeneralRe: Help with threads Pin
FISH78610-Jul-09 0:34
FISH78610-Jul-09 0:34 
AnswerRe: Help with threads Pin
Rajesh R Subramanian9-Jul-09 22:52
professionalRajesh R Subramanian9-Jul-09 22:52 
GeneralRe: Help with threads Pin
Roger Stoltz10-Jul-09 1:03
Roger Stoltz10-Jul-09 1:03 
GeneralRe: Help with threads Pin
Rajesh R Subramanian10-Jul-09 1:08
professionalRajesh R Subramanian10-Jul-09 1:08 
Questionmfc tab problem Pin
DevelopmentNoob9-Jul-09 16:15
DevelopmentNoob9-Jul-09 16:15 
AnswerRe: mfc tab problem Pin
«_Superman_»9-Jul-09 19:47
professional«_Superman_»9-Jul-09 19:47 
GeneralRe: mfc tab problem Pin
DevelopmentNoob16-Jul-09 21:18
DevelopmentNoob16-Jul-09 21:18 
QuestionIs it safe to cast from LPBYTE to LPSTR to LPBYTE ? Pin
Mike the Red9-Jul-09 13:47
Mike the Red9-Jul-09 13:47 
AnswerRe: Is it safe to cast from LPBYTE to LPSTR to LPBYTE ? Pin
Stuart Dootson9-Jul-09 14:19
professionalStuart Dootson9-Jul-09 14:19 
JokeThanks, Stuart! -nt- Pin
Mike the Red9-Jul-09 15:43
Mike the Red9-Jul-09 15:43 
QuestionFormat CEdit for Phone Number Pin
DanYELL9-Jul-09 12:04
DanYELL9-Jul-09 12:04 
AnswerRe: Format CEdit for Phone Number Pin
David Crow9-Jul-09 16:11
David Crow9-Jul-09 16:11 
GeneralRe: Format CEdit for Phone Number Pin
kilt9-Jul-09 23:25
kilt9-Jul-09 23:25 
GeneralRe: Format CEdit for Phone Number Pin
DanYELL10-Jul-09 1:46
DanYELL10-Jul-09 1:46 
QuestionRe: Format CEdit for Phone Number Pin
David Crow10-Jul-09 2:52
David Crow10-Jul-09 2:52 
AnswerRe: Format CEdit for Phone Number Pin
DanYELL10-Jul-09 3:28
DanYELL10-Jul-09 3:28 
GeneralRe: Format CEdit for Phone Number Pin
David Crow10-Jul-09 3:38
David Crow10-Jul-09 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.