diff --git a/ortools/util/fp_utils.cc b/ortools/util/fp_utils.cc index 734b2f031f..f40a81dad8 100644 --- a/ortools/util/fp_utils.cc +++ b/ortools/util/fp_utils.cc @@ -22,7 +22,6 @@ #include #include #include -#include #include "absl/base/casts.h" #include "absl/log/check.h" diff --git a/ortools/util/fp_utils.h b/ortools/util/fp_utils.h index ad2718b1dd..4590ce1167 100644 --- a/ortools/util/fp_utils.h +++ b/ortools/util/fp_utils.h @@ -29,7 +29,6 @@ #include // Needed before fenv_access. See https://github.com/microsoft/STL/issues/2613. #include // IWYU pragma:keep. -#include #include "absl/log/check.h" #include "absl/types/span.h" diff --git a/ortools/util/sigint.cc b/ortools/util/sigint.cc index b807372d65..601f4983cc 100644 --- a/ortools/util/sigint.cc +++ b/ortools/util/sigint.cc @@ -23,15 +23,18 @@ namespace operations_research { void SigintHandler::Register(const std::function& f) { handler_ = [this, f]() -> void { - ++num_sigint_calls_; - if (num_sigint_calls_ >= 3) { - LOG(INFO) << "^C pressed " << num_sigint_calls_ - << " times. Forcing termination."; + const int num_sigint_calls = ++num_sigint_calls_; + if (num_sigint_calls < 3) { + LOG(INFO) + << "^C pressed " << num_sigint_calls << " times. " + << "Interrupting the solver. Press 3 times to force termination."; + if (num_sigint_calls == 1) f(); + } else if (num_sigint_calls == 3) { + LOG(INFO) << "^C pressed 3 times. Forcing termination."; exit(EXIT_FAILURE); + } else { + // Another thread is already running exit(), do nothing. } - LOG(INFO) << "^C pressed " << num_sigint_calls_ << " times. " - << "Interrupting the solver. Press 3 times to force termination."; - if (num_sigint_calls_ == 1) f(); }; signal(SIGINT, &ControlCHandler); } diff --git a/ortools/util/sigint.h b/ortools/util/sigint.h index 457de9bd67..7b3098033e 100644 --- a/ortools/util/sigint.h +++ b/ortools/util/sigint.h @@ -14,6 +14,7 @@ #ifndef OR_TOOLS_UTIL_SIGINT_H_ #define OR_TOOLS_UTIL_SIGINT_H_ +#include #include namespace operations_research { @@ -30,7 +31,7 @@ class SigintHandler { private: static void ControlCHandler(int s); - int num_sigint_calls_ = 0; + std::atomic num_sigint_calls_ = 0; thread_local static std::function handler_; };