Hello,
I am facing 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 is running fine and as expected, but when it's exiting and terminating by catching the signal SIGINT, the console output afterwards is hidden (i.e. the console/terminal behaves like applying the command "stty -echo").
I've already inserted the code 'system ("stty echo");' in the function CleanupHandler before exiting and it "solved" the problem, but this can't be the right solution... Perhaps I am missing a severe fault that just doesn't show up. I also just don't figure out how my code affects the console output/terminal itself after terminating, because there isn't any system call/function call that may alter console behaviour (I am only using "printf" and color formatting for console output).
The code snippet above should reproduce the problem, compiler/linker configuration and output was:
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
I am developing remotely on a server, problem occurs this way and over SSH, too, so this can't be any console bug. Additionally, putting console input handler in main thread and core functionality/loop in child thread doesn't make a difference.
Thanks in advance for any help.