Create a blocking function for a thread
-
i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now:
BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); .....
if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D [] -
i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now:
BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); .....
if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []I think you should perhaps insert a Sleep command inside your thread for not to consume so much processor power. Beside that, I don't know whether your thread-solution is the most practible one when waiting for a variable to change. Perhaps you could work with events and one of the wait functions (like WaitForSingleObject), setting the event when your computations have finished (take care: in that case your computation should be placed inside the thread because the wait function is blocking)
-
i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now:
BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); .....
if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []Spinning in a tight loop waiting for a variable to change is not the proper way to notify a thread to begin doing something. Read up on thread synchronization techniques; events and semaphores and the method WaitForSingleObject. If you must do it the way you've described, put a tiny sleep (Sleep(10)) in your while loop. Ron Ward
-
Spinning in a tight loop waiting for a variable to change is not the proper way to notify a thread to begin doing something. Read up on thread synchronization techniques; events and semaphores and the method WaitForSingleObject. If you must do it the way you've described, put a tiny sleep (Sleep(10)) in your while loop. Ron Ward
the reason why i want it to be a blocking process is because i want to recreate the windows messaging device, so make my own messagelist. this list will be read by a procedure for the control of a database. i wanted to make the function that reads the messagelist like the function
recv
(for network communication), the functionrecv
lockes up thewhile
loop until it has received information. i hope that something like this is possible... []D [] []D [] -
the reason why i want it to be a blocking process is because i want to recreate the windows messaging device, so make my own messagelist. this list will be read by a procedure for the control of a database. i wanted to make the function that reads the messagelist like the function
recv
(for network communication), the functionrecv
lockes up thewhile
loop until it has received information. i hope that something like this is possible... []D [] []D []i don't really get what ur doing but maybe this is sort of what u are looking for? UINT WatchThread(LPVOID param) { while(1){ myclass* pthis = (myclass*)param; int nmsg = pthis->getMsg(); // do something here } } int cmyclass::getMsg(){ ::WaitForSingleObject( _event, INFINITE); return _msgNumber; } void myclass::setMsg( int nmsg) { // lock a critical section or use whatever synchronization needed _msgNumber = nmsg; SetEvent( _event); // unlock the critical section or whatever }
"No matter where you go, there your are..." - Buckaoo Banzi
-pete
-
i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now:
BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); .....
if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []