0

I am developing a windows app in C++ in VS2017 I have to perform some data(stored in queue) processing in a thread, but only when items exist in a queue.

thread
{
if(!queue.empty()
{
//process data and pop out 
}
}

But there are chances when queue mayb empty and sometimes have data, so how can i acheive this?

I tried to run whole loop in thread , but that isn't effective and optimized ad application becomes unresponsive after some time

 thread
{
    while(true)
    {
        if(!queue.empty()
        {
            //process data and pop out 
        }
    }
}

Please support and help in knowing how can this be done effectively?

pushE
  • 394
  • 3
  • 16
  • 1
    That's not valid C++... – Shawn Sep 17 '18 at 04:39
  • I just wrote generic code,not actual C++. But actually i am using C++ – pushE Sep 17 '18 at 04:40
  • 1
    You should post actual compileable code. People will be more inclined to help and less inclined to downvote. – Shawn Sep 17 '18 at 04:43
  • 1
    Possible duplicate of [Using condition variable in a producer-consumer situation](https://stackoverflow.com/questions/2379806/using-condition-variable-in-a-producer-consumer-situation) – 1201ProgramAlarm Sep 17 '18 at 04:47
  • Adding and removing elements from the queue is a mutex-protected operation. If the queue was empty before a new element was added, a new thread gets started. The thread examines, but does not remove the first element in the queue, then does what it needs to do with it. The thread removes the element from the queue (a mutex-protected operation) after it's done with it, and if the queue is now empty, the thread terminates, otherwise the thread works the next element in the queue. If you have any more questions, [go talk to your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – Sam Varshavchik Sep 17 '18 at 04:48
  • You might also be interested in [Thread pooling in C++11](https://stackoverflow.com/q/15752659/608639). I feel like we need to see something more than pseudo-code. *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve).* – jww Sep 17 '18 at 04:53

2 Answers2

1

What you seek is a thing called a condition_variable. It will allow your worker thread to pause when it has nothing to do and then resume as soon as another thread inserts something into the queue (and notifies your condition_variable to unblock).

Examples here and here are better than anything I could write up.

selbie
  • 100,020
  • 15
  • 103
  • 173
0

Answer from @selbie helped to do this as follows

thread
{
    while(true)
    {
        if(condition_varibale not TURE wait here)

        if(!queue.empty()
        {
            //process data and pop out 
        }
    }
}

update condition variable when adding items to queue.

pushE
  • 394
  • 3
  • 16