Reputation: 45
So I wrote a multi-thread program using boost::lockfree::queue, and the code is pretty much the same as the given example http://www.boost.org/doc/libs/1_62_0/doc/html/lockfree/examples.html. Well, I have 4 queues and the data is a struct rather than int.
The problem is that my program eats up 95% of my cpu when running and it's super slow. After a little investigation I found out that boost::lockfree::queue::pop takes 80% of the cpu usage, which isn't surprising because of these two loops
while (!done) {
while (queue.pop(value))
++consumer_count;
}
Is there anything I can do to reduce the cpu usage, or should I upgrade my CPUs? I am using boost 1.61, visual studio 2015 on windows 10, btw.
Thanks so much
Upvotes: 1
Views: 687
Reputation: 783
What did you expected? The following piece of code is the same as yours (in terms of eating CPU)
int counter = 0;
bool condition_1 = false;
bool condition_2 = false;
while(!condition_1) {
while(!condition_2) {
++counter;
condition_2 = true;
}
}
What you need, IMHO, is some notification mechanism (e.g. condition_variable), when poll queue. Otherwise, constantly polling it, obviously, eats cpu.
Upvotes: 1