making an non-blocking call in VC++
-
Hi to All, I have got a message Queue. A thread function which should be able to process the queue, (that is if any data is available, then do some processing)....otherwise do nothing.. Now, my problem is...I cannot keep a while loop (inside the thread) and keep checking for the queue if there is any element in the queue...because it will consume lots of CPU processing power....Instead, I found the best method is to ......have a method, say getq()...which can be a blocking call...ie, inside my thread while loop , I should be able to do something like threadFunction() { while(1) { if(getq() != 0) { } } } so the functionality will be...it will go inside(if block)only when there is an element in the queue...otherwise wait there... Now, I need help/hints to make the getq function...(which will return any value only if there is any elemt in the queue...otherwise it will not return the control...It should keep waiting till any element is there in queue ) thanks in advance
----------------------------- I am a beginner
-
Hi to All, I have got a message Queue. A thread function which should be able to process the queue, (that is if any data is available, then do some processing)....otherwise do nothing.. Now, my problem is...I cannot keep a while loop (inside the thread) and keep checking for the queue if there is any element in the queue...because it will consume lots of CPU processing power....Instead, I found the best method is to ......have a method, say getq()...which can be a blocking call...ie, inside my thread while loop , I should be able to do something like threadFunction() { while(1) { if(getq() != 0) { } } } so the functionality will be...it will go inside(if block)only when there is an element in the queue...otherwise wait there... Now, I need help/hints to make the getq function...(which will return any value only if there is any elemt in the queue...otherwise it will not return the control...It should keep waiting till any element is there in queue ) thanks in advance
----------------------------- I am a beginner
This way you're simply 'passing the problem' to
getq
method. BTW when the queue is empty your thread may go to sleep (i.e. callSleep
[^]) to release theCPU
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi to All, I have got a message Queue. A thread function which should be able to process the queue, (that is if any data is available, then do some processing)....otherwise do nothing.. Now, my problem is...I cannot keep a while loop (inside the thread) and keep checking for the queue if there is any element in the queue...because it will consume lots of CPU processing power....Instead, I found the best method is to ......have a method, say getq()...which can be a blocking call...ie, inside my thread while loop , I should be able to do something like threadFunction() { while(1) { if(getq() != 0) { } } } so the functionality will be...it will go inside(if block)only when there is an element in the queue...otherwise wait there... Now, I need help/hints to make the getq function...(which will return any value only if there is any elemt in the queue...otherwise it will not return the control...It should keep waiting till any element is there in queue ) thanks in advance
----------------------------- I am a beginner
You have to use events for that: you can wait (without consuming any CPU time) on an event to be notified. Typically what happens is that you have a thread pushing elements into your queue and a thread popping element from the queue. The thread popping elements wait on an event to be signaled by the other thread after he pushed something into the queue. You will also need to add proper synchronization on your queue to avoid accessing it from both threads at the same time. I suggest you start by reading this article[^] which will give you an overview of threads and synchronization techniques.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Hi to All, I have got a message Queue. A thread function which should be able to process the queue, (that is if any data is available, then do some processing)....otherwise do nothing.. Now, my problem is...I cannot keep a while loop (inside the thread) and keep checking for the queue if there is any element in the queue...because it will consume lots of CPU processing power....Instead, I found the best method is to ......have a method, say getq()...which can be a blocking call...ie, inside my thread while loop , I should be able to do something like threadFunction() { while(1) { if(getq() != 0) { } } } so the functionality will be...it will go inside(if block)only when there is an element in the queue...otherwise wait there... Now, I need help/hints to make the getq function...(which will return any value only if there is any elemt in the queue...otherwise it will not return the control...It should keep waiting till any element is there in queue ) thanks in advance
----------------------------- I am a beginner
If you're using MFC, you could simply derive a class from
CWinThread
. It's really simple and you don't need to maintain a queue or anything. MFC does everything for you. You could use PostThreadMessage[^] to post a message to the message queue of the thread. I have a feeling that the family ofWaitFor...
functions may be of help to you if you aren't using MFC!“Follow your bliss.” – Joseph Campbell
-
Hi to All, I have got a message Queue. A thread function which should be able to process the queue, (that is if any data is available, then do some processing)....otherwise do nothing.. Now, my problem is...I cannot keep a while loop (inside the thread) and keep checking for the queue if there is any element in the queue...because it will consume lots of CPU processing power....Instead, I found the best method is to ......have a method, say getq()...which can be a blocking call...ie, inside my thread while loop , I should be able to do something like threadFunction() { while(1) { if(getq() != 0) { } } } so the functionality will be...it will go inside(if block)only when there is an element in the queue...otherwise wait there... Now, I need help/hints to make the getq function...(which will return any value only if there is any elemt in the queue...otherwise it will not return the control...It should keep waiting till any element is there in queue ) thanks in advance
----------------------------- I am a beginner
Hi, You might consider using a semaphore to control queuing/de-queuing so that the de-queuing thread does not consume any CPU while it is waiting for more items to process. There is an exellent Queue class on this site: Using Semaphores: Multithreaded Producer/Consumer By Joseph M. Newcomer Its slick and very compact - you wont get much better that this. Have fun. :-D Tony