I have a process which need to listen and handle.
to be specific, I need to listen from two shared memory, and whenever there are new data arrived. I need to handle it. meanwhile, i don't want one data block the other, so, I let them run in separate thread.
here is a demo code:
void RunFirst() {
while (true) {
std::string s;
shm1_->Recv(&s);
// handle 1
}
}
void RunSecond() {
while (true) {
std::string s;
shm2_->Recv(&s);
// handle 2
}
}
void start() {
std::thread t(RunFirst);
RunSecond();
}
I have two shm object, which are two shared memory Recviver. they loop to listen if there is data.
it runs well which can handle whenever data comes. and wont let one data blocks another.
but the problem is the high CPU usage. it make two CPU runs in 100% usage.
Is there any methods can decrease CPU usage and keep the unblock?