1) I'm new to std::thread and I would like to know whether it is a good practice to call pthread_sigmask()
to block some signals in a particular thread created by std::thread
.
I don't want the new thread to receive signals such as SIGTERM, SIGHUP, etc., because the main process has already installed handlers for these signals.
So, is it a good practice to call pthread_sigmask()
to block some signals in a thread created by std::thread
?
2) Also, I believe the effect of pthread_sigmask(SIG_BLOCK, &mask, NULL)
will only apply to the thread created using
std::thread(&Log::rotate_log, this, _logfile, _max_files, _compress).detach();
and calling rotate_log()
as the start function.
And the effect of pthread_sigmask(SIG_BLOCK, &mask, NULL)
will not apply to the thread where std::thread(&Log::rotate_log, this, _logfile, _max_files, _compress).detach()
is called.
Is my understanding correct?
void rotate_log (std::string logfile, uint32_t max_files, bool compress)
{
sigset_t mask;
sigemptyset (&mask);
sigaddset (&mask, SIGTERM);
sigaddset (&mask, SIGHUP);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
// Do other stuff.
}
void Log::log (std::string message)
{
// Lock using mutex
std::lock_guard<std::mutex> lck(mtx);
_outputFile << message << std::endl;
_outputFile.flush();
_sequence_number++;
_curr_file_size = _outputFile.tellp();
if (_curr_file_size >= max_size) {
// Code to close the file stream, rename the file, and reopen
...
// Create an independent thread to compress the file since
// it takes some time to compress huge files.
if (!_log_compression_on)
{
std::thread(&Log::rotate_log, this, _logfile, _max_files, _compress).detach();
}
}
}