Skip to content

Commit

Permalink
Merge pull request #882 from redboltz/fix_bench
Browse files Browse the repository at this point in the history
Used compare_exchange_weak for decriment and comparison.
  • Loading branch information
redboltz authored Oct 11, 2021
2 parents f83cd91 + 56faa3d commit 599ee0b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions example/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

namespace as = boost::asio;

template <typename T>
bool compare_decrement(std::atomic<T>& val, T comp) {
T old = val.load();
do {
if (old == comp) return true;
} while (!val.compare_exchange_weak(old, old - 1));
return false;
}

int main(int argc, char **argv) {
try {
boost::program_options::options_description desc;
Expand Down Expand Up @@ -568,9 +577,7 @@ int main(int argc, char **argv) {
async_wait_pub(ci);
}

if (--rest_times == 0) {
finish_proc();
}
if (compare_decrement(rest_times, std::uint64_t(1))) finish_proc();
return true;
};

Expand All @@ -585,7 +592,7 @@ int main(int argc, char **argv) {
[&]
(bool /*sp*/, MQTT_NS::connect_return_code connack_return_code) {
if (connack_return_code == MQTT_NS::connect_return_code::accepted) {
if (--rest_connect == 0) sub_proc();
if (compare_decrement(rest_connect, std::size_t(1))) sub_proc();
}
else {
std::cout << "connack error:" << connack_return_code << std::endl;
Expand All @@ -597,7 +604,7 @@ int main(int argc, char **argv) {
[&]
(bool /*sp*/, MQTT_NS::v5::connect_reason_code reason_code, MQTT_NS::v5::properties /*props*/) {
if (reason_code == MQTT_NS::v5::connect_reason_code::success) {
if (--rest_connect == 0) sub_proc();
if (compare_decrement(rest_connect, std::size_t(1))) sub_proc();
}
else {
std::cout << "connack error:" << reason_code << std::endl;
Expand All @@ -613,7 +620,7 @@ int main(int argc, char **argv) {
if (results.front() == MQTT_NS::suback_return_code::success_maximum_qos_0 ||
results.front() == MQTT_NS::suback_return_code::success_maximum_qos_1 ||
results.front() == MQTT_NS::suback_return_code::success_maximum_qos_2) {
if (--rest_sub == 0) pub_proc();
if (compare_decrement(rest_sub, std::size_t(1))) pub_proc();
}
return true;
}
Expand All @@ -627,7 +634,7 @@ int main(int argc, char **argv) {
if (reasons.front() == MQTT_NS::v5::suback_reason_code::granted_qos_0 ||
reasons.front() == MQTT_NS::v5::suback_reason_code::granted_qos_1 ||
reasons.front() == MQTT_NS::v5::suback_reason_code::granted_qos_2) {
if (--rest_sub == 0) pub_proc();
if (compare_decrement(rest_sub, std::size_t(1))) pub_proc();
}
return true;
}
Expand Down

0 comments on commit 599ee0b

Please sign in to comment.