Discard a problem in a console application (Linux)

There is a strange problem in my console application.

First of all, the code snippet:

main.cpp

#include "DebugInterface.h"

static sigset_t signalSet;
static pthread_t CleanupHandlerThread;
DebugInterface* debugInterface = NULL;

void* CleanupHandler (void* param) {
    int32_t sig, err;
    err = sigwait (&signalSet, &sig);

    delete debugInterface;
    debugInterface = NULL;
    exit (EXIT_SUCCESS);

    return NULL;
}

int32_t main(int32_t argc, char** argv) {
    sigemptyset (&signalSet);
    sigaddset (&signalSet, SIGINT);
    pthread_sigmask (SIG_BLOCK, &signalSet, NULL);
    pthread_create (&CleanupHandlerThread, NULL, CleanupHandler, NULL);

    debugInterface = new DebugInterface();

    if (debugInterface != NULL) {
        debugInterface->StartReading();
    }

    while (true) {
        // Core functionality follows, but was commented out
        // (was not relevant for problem, checked it -
        // you may even remove this loop completely)
    }

    return EXIT_SUCCESS;
}

DebugInterface.h

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>

class DebugInterface {
public:
    DebugInterface() { }
    virtual ~DebugInterface() { }

private:
    pthread_t ConsoleHandlerThread;

private:
    static void* ConsoleHandler (void* param);

public:
    bool StartReading();
};

DebugInterface.cpp

#include "DebugInterface.h"

void* DebugInterface::ConsoleHandler (void* param) {
    while (true) {
        char* input = readline ("\n> ");

        // Handle input, was commented out, too (irrelevant for problem)

        free (input);
    }

    return NULL;
}

bool DebugInterface::StartReading() {
    if (pthread_create (&ConsoleHandlerThread, NULL, DebugInterface::ConsoleHandler, NULL) != 0) {
        return false;
    }
    return true;
}

The code works fine and, as expected, but when it exits and exits, catching the SIGINT signal, the console output is subsequently hidden (that is, the console / terminal behaves like applying the "stty -echo" command).

( "stty echo" ); CleanupHandler "" , ... , , . , / , /, ( "printf" ).

, / :

make all 
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
Finished building: ../main.cpp

Building target: Testproject
Invoking: GCC C++ Linker
g++  -o"Testproject"  ./DebugInterface.o ./main.o   -lreadline -lpthread
Finished building target: Testproject

, SSH, . , / .

.

+3
3

. readline.

, readline

+4

strace, , .

+1

, GNU Readline, .

"delete debugInterface;":

rl_cleanup_after_signal();
rl_free_line_state();

[...]

delete debugInterface;
0

Source: https://habr.com/ru/post/1713128/


All Articles