1

In one of the member functions (being accessed by multiple threads) of my class, I create a separate thread to compress huge log file (~1GB).

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();
            }
        }
    }

I don't want this new thread to receive signals such as SIGTERM, SIGHUP, etc., because the main process has already installed handlers for these signals.

I want to know how can I block some signals in the newly created std::thread.

void rotate_log (std::string logfile, uint32_t max_files, bool compress)
{
    // How to block some signals such as SIGTERM, SIGHUP?

    // Do other stuff.
}
void
  • 338
  • 5
  • 19
  • 2
    Please read more about [signals in general](http://man7.org/linux/man-pages/man7/signal.7.html), and learn about *signal masks*. And note that not all signals may be sent to child-threads. (I know that the linked manual page is Linux-specific, but most of the information in it is common for all POSIX platforms, and most of that information is valid for other POSIX platforms as well.) – Some programmer dude Feb 25 '19 at 12:59
  • I read that. However, I have further questions that I posted at: https://stackoverflow.com/questions/54871085/is-it-a-good-practice-to-call-pthread-sigmask-in-a-thread-created-by-stdthread – void Feb 25 '19 at 17:04

0 Answers0