Threads
-
Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object
-
Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object
Remember when I said "When messages are sent between threads, it will block the sending thread and cause a context switch to the receiver, which nullifies the point of making the app multi-threaded"? Guess what's happening in your app...
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Remember when I said "When messages are sent between threads, it will block the sending thread and cause a context switch to the receiver, which nullifies the point of making the app multi-threaded"? Guess what's happening in your app...
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object
I am afraid, i can not accommodate 100000000000. :sigh:
We Believe in Excellence www.aqueelmirza.cjb.net
-
Is the best way to let the Frames & Views in the standard thread and create only for the "hard working functions" a Worker-Thread ? Thanks
baerten wrote:
Is the best way to let the Frames & Views in the standard thread and create only for the "hard working functions" a Worker-Thread ?
To put it short: Yes! The most common reason to use worker threads is to keep the GUI responsive during operations that will take a lot of time. The task is handed over to a thread that usually posts(!) a message to the main thread when it's done. The main thread is the one that also handles the GUI. The use of worker threads for solving other kind of problems should be carefully considered in order to prevent design flaws and performance bottlenecks. The most common mistake related to multithreading is the use of ::SendMessage() between threads. As Mike said "it will block the sending thread and cause a context switch to the receiver, which nullifies the point of making the app multi-threaded". This will cause a deadlock, it's just a matter of time. The remedy for this is to use ::PostMessage() instead. This is also the reason that the GUI shall never be manipulated from a secondary thread using MFC since those calls will result in a call to ::SendMessage(). Keep in mind that multithreading is not the same as real multitasking. My point is that if you post a message to a thread, the message will not be handled until the receiving thread is given a time slice to run in by the OS. This means that you have to expect a latency before action is taken on the request that is represented by the posted message. Nowdays you can assume this time to be about 10ms under normal circumstances. For Win95 it was about 55ms. Regarding your initial post there might be other design flaws or bugs that are not related to the use of multiple threads. Aqueel's post points out one. Even if your variable could hold a value that big, you can expect your application to freeze and the GUI to be non-responsive since your burning a lot of MIPS in a UI-handler for a button click event. Hope this helps -- Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998
"...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above -
Hi, i made this structure of my project FRAME -> THREAD -> VIEW ( -> = creates ) In the Taskmanager i see that a new Thread is created if i open the Frame ( MDI-Child ) In Spy++ it's the same, 3 Threads for all the application, in the "ViewThread" i see the components So far so good But if i execute a nice loop on a Button-Click int u=0; for(int i=0; i<100000000000; i++) u=0; the entire application is freezed. But this code is located in the CFormView object which is a part of the Thread ... Why does all the application freeze? Many thanks :) PS: The Thread is a derived class from CWinThread In the InitInstance() function i create the CFormView object