Closed liuxw7 closed 2 years ago
I've written about the need for the extra compare_exchange_strong
in this blog. If that isn't clear, I can elucidate further.
Perfect! When I tested it, I found CPU is very high: 100%, 75%. How to improve it?
@liuxw7 That's mostly due to pub-sub being a poll, rather than a signal. The ideal way would be to limit the rate at which spin
is called:
while (true) {
std::this_thread::sleep_for(milliseconds(10));
subscriber.spin_once();
}
The above will run the subscriber 100 times per second (ignoring the time taken for callback). This will reduce CPU usage.
I'm currently trying to figure out ways to replace this poll with a signal (similar to how RPC works), but for now, rate limiting is your best option.