Skip to content
Open

fix UB #1276

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions code/Examples/C++/RpcClientServerBasic/src/CalculatorServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <atomic>
#include <chrono>
#include <csignal>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
Expand Down Expand Up @@ -143,12 +144,13 @@ class Server
//!--
};

std::function<void(int)> stop_handler;
volatile std::sig_atomic_t stop_requested = 0;

void signal_handler(
int signum)
{
stop_handler(signum);
(void)signum;
stop_requested = 1;
}

//!--MAIN
Expand All @@ -163,12 +165,6 @@ int main(

std::thread thread(&Server::run, server);

stop_handler = [&](int signum)
{
std::cout << "Signal received, stopping execution." << std::endl;
server->stop();
};

signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#ifndef _WIN32
Expand All @@ -178,8 +174,12 @@ int main(

std::cout << "Server running. Please press Ctrl+C to stop the server at any time." << std::endl;

while (!stop_requested)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << "Signal received, stopping execution." << std::endl;
server->stop();
thread.join();

return 0;
}
//!--