Multithreading
-
IN a Dialog I have two CButton when i clicked on first button a loop starts. The requirement is when i click on second button it should break the loop. How i can communicate this to the loop. I tried Sleep so that i can check the status of common variable. But this variable is updatted only when the loop terminates. What will be the possible solution of my Problem.
-
IN a Dialog I have two CButton when i clicked on first button a loop starts. The requirement is when i click on second button it should break the loop. How i can communicate this to the loop. I tried Sleep so that i can check the status of common variable. But this variable is updatted only when the loop terminates. What will be the possible solution of my Problem.
one thing that you can do is that create a static flag=false as a global variable. now in the loop you can check that if the flag is set to true. (You will set the flag to true when the second button is pressed.) if the flag is set simply put return 0; in the block of code where you want the thread function to end. Here's some code for you that is inside the thread function :
// Code inside the thread function and m_number is the //variable for the edit box whose number property is set to true CMyDlg *test=(CMyDlg *)pVoid; for(int i=0;i<1000;i++) { Sleep(10); if(flag==true) { CString s; s.Format ("%d",i); AfxMessageBox("Thread stoping on "+ s); return 0; } else { CString s; s.Format ("%d",i); test->m_number.SetWindowText (s); } } return 0;
Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_ -- modified at 9:34 Tuesday 13th June, 2006
-
IN a Dialog I have two CButton when i clicked on first button a loop starts. The requirement is when i click on second button it should break the loop. How i can communicate this to the loop. I tried Sleep so that i can check the status of common variable. But this variable is updatted only when the loop terminates. What will be the possible solution of my Problem.
The old way (Win16) of doing this was to pump messages in the loop so that the other button will respond to user input. Today, a worker thread would be what I would suggest. Look up how to create a background thread using the links provided in the previous answer. While you can use a shared variable to terminate the thread, I would suggest looking into how "event" objects work with threads to get a feel for them and how they should be used. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
IN a Dialog I have two CButton when i clicked on first button a loop starts. The requirement is when i click on second button it should break the loop. How i can communicate this to the loop. I tried Sleep so that i can check the status of common variable. But this variable is updatted only when the loop terminates. What will be the possible solution of my Problem.
Create an event handle (using CreateEvent). Pass that handle to the loop when you create it (its quite common to pass such objects in the LPARAM pointer used when creating a thread). When you need to terminate the thread, call SetEvent. Internally to the loop, you will use WaitForSingleObject with an appropriate timeout value (in your case, 0 would probably work since you just want to check to make sure it has already been set). If WaitForSingleObject returns WAIT_OBJECT_0, then your event was signaled and you should stop the loop. Alternatively, you could create a boolean flag and put a critical section around every access. This works just as well, but is error prone (one access without a critical section breaks the entire system). The Event solution doesn't have that problem. If you use Sleep in any application, you can pretty much be assured that your design is flawed. Using it in Multithreaded applications to try to manage synchronization between threads is worse than useless. Use proper thread synchronization techniques and you won't have issues. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Create an event handle (using CreateEvent). Pass that handle to the loop when you create it (its quite common to pass such objects in the LPARAM pointer used when creating a thread). When you need to terminate the thread, call SetEvent. Internally to the loop, you will use WaitForSingleObject with an appropriate timeout value (in your case, 0 would probably work since you just want to check to make sure it has already been set). If WaitForSingleObject returns WAIT_OBJECT_0, then your event was signaled and you should stop the loop. Alternatively, you could create a boolean flag and put a critical section around every access. This works just as well, but is error prone (one access without a critical section breaks the entire system). The Event solution doesn't have that problem. If you use Sleep in any application, you can pretty much be assured that your design is flawed. Using it in Multithreaded applications to try to manage synchronization between threads is worse than useless. Use proper thread synchronization techniques and you won't have issues. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Zac Howland wrote:
Alternatively, you could create a boolean flag and put a critical section around every access. This works just as well, but is error prone (one access without a critical section breaks the entire system). The Event solution doesn't have that problem.
You can also go the shared variable route and use things like
InterlockedCompareExchange(...)
(CMPXCHG
) as well. One thing though - for this particular kind of situation, I am not sure that synchronized access is required. Since the value is not being used as a counter, or anything like that but just a simple flag, it should be OK to have one thread reading it waiting for its value to change while another threads writes it (once) to set that value. I do not think that the reading of the value will interfere with the writing of it, and the thread would catch the change on the next read. (Assuming automatic volatile handling and things like that.) And yes, that was my 5. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
IN a Dialog I have two CButton when i clicked on first button a loop starts. The requirement is when i click on second button it should break the loop. How i can communicate this to the loop. I tried Sleep so that i can check the status of common variable. But this variable is updatted only when the loop terminates. What will be the possible solution of my Problem.
As your thread title says, you should go for multithreading rather than the sleeping business. But what's the nature of the loop? can it be done with a Timer? I used to create threads unnecessarily when it could be done with timers :sigh:. So see to that it cannot be done with a timer first then go for multithreading. As said by David, never get your Sleep inside your thread or any loop, it puts the entire application to sleep :zzz:
--[:jig:]-- [My Current Status]
-
one thing that you can do is that create a static flag=false as a global variable. now in the loop you can check that if the flag is set to true. (You will set the flag to true when the second button is pressed.) if the flag is set simply put return 0; in the block of code where you want the thread function to end. Here's some code for you that is inside the thread function :
// Code inside the thread function and m_number is the //variable for the edit box whose number property is set to true CMyDlg *test=(CMyDlg *)pVoid; for(int i=0;i<1000;i++) { Sleep(10); if(flag==true) { CString s; s.Format ("%d",i); AfxMessageBox("Thread stoping on "+ s); return 0; } else { CString s; s.Format ("%d",i); test->m_number.SetWindowText (s); } } return 0;
Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_ -- modified at 9:34 Tuesday 13th June, 2006
-
As your thread title says, you should go for multithreading rather than the sleeping business. But what's the nature of the loop? can it be done with a Timer? I used to create threads unnecessarily when it could be done with timers :sigh:. So see to that it cannot be done with a timer first then go for multithreading. As said by David, never get your Sleep inside your thread or any loop, it puts the entire application to sleep :zzz:
--[:jig:]-- [My Current Status]
(Assuming you are talking about
Sleep(...)
...)VuNic wrote:
As said by David, never get your Sleep inside your thread or any loop, it puts the entire application to sleep
No, it just the thread it is called on... An application may consist of multiple threads.
Sleep(...)
only affects one particular thread. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
It was just to show the user. Afterall this was not a live example. It was just to give the user the time to perform the inputs while the thread is running Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
(Assuming you are talking about
Sleep(...)
...)VuNic wrote:
As said by David, never get your Sleep inside your thread or any loop, it puts the entire application to sleep
No, it just the thread it is called on... An application may consist of multiple threads.
Sleep(...)
only affects one particular thread. Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)Thanks james, I thought Sleep is global call. hmm.. thanks for the info.. nice update for me.
--[:jig:]-- [My Current Status]