Click here to Skip to main content
15,889,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing coding in vs 2008.

My code is:
C++
queue<int> globalqueue;
DWORD WINAPI popq(void *vp)
{
    while(1)
    {
        while(globalqueue.size() >0 )
        {
            EnterCriticalSection(&section);
            if( myqueue.size() > 0 )
            {
                printf("%d\n", myqueue.front());
                myqueue.pop();
            }
            LeaveCriticalSection(&section);
        }
        sleep(1000*60);
    }
    return 0;
}

Another function will insert into the queue. If data is not available in the queue to pop I am sleeping for 60 sec.
Here i don't want to sleep. If any data I am pushing into the queue I need to wake up the thread immediately.

Is there any way to achieve that?
Posted
Updated 11-Jul-13 4:51am
v4
Comments
SoMad 11-Jul-13 3:44am    
Look into WaitForSingleObject() (and WaitForMultipleObjects()), using an event to signal your thread to resume processing.

Soren Madsen
[no name] 11-Jul-13 4:12am    
This is the correct answer. This approach will also allow you to stop your thread gracefully which your current construct does not allow.
@BangIndia 11-Jul-13 5:05am    
which handle i have to use inside the WaitForSingleObject() ..
Its require a object rite.
SoMad 11-Jul-13 5:16am    
That would typically be a handle to an event. See this complete sample using WaitForMultipleObjects(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915(v=vs.85).aspx[^]

Soren Madsen

I have recently answered a question that contains a blocking queue template that probably suits your needs perfectly: Multi producer/consumer threads with condition variables...why don't work well?[^]. Read the answer carefully as it may contain a lot of useful info for you too. The stuff you need from my answer is the BlockingQueue template. You can interrupt the sleep at the end of program execution by placing a special item into the queue - in my example I used a NULL pointer for this.

Regarding the CRITICAL_SECTION + windows event combo: Events and critical sections (critical section == lock == mutex) are often used together to do synchronization between multiple threads. An event that is triggered multiple times (not just once) is almost always used with an accompanying lock (critical section or mutex or whatever). If you are a beginner I have a very nice advice for you regarding the use of criticalsection+event combo: Always wait for the event when you don't hold the lock, and always set/reset the event while you are holding the lock. (On holding the lock I mean being entered in the criticalsection). There are some exceptions to this rule but this one helped me a lot in writing correct code when I began with threading.
 
Share this answer
 
v5
Comments
nv3 11-Jul-13 5:46am    
My 5.
pasztorpisti 11-Jul-13 6:02am    
Thank you!
you can use WaitForMultipleObjects() API which also has a timeout. With the SetEvent() API you can trigger a loop.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900