Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:32:05+00:00 2026-05-28T03:32:05+00:00

I have a thread in an application that has a loop like this: …

  • 0

I have a thread in an application that has a loop like this:

...
while (1)
{
    checkDatabase();
    checkChildren();

    sleep(3);
}
...

checkDatabase() is self-explanatory; checkChildren() simply calls waitpid(-1, &status, WNOHANG) to deal with child processes that have either exited or received a signal.

The application works fairly well, but it has default signal handling. The problem is that this parent process has a number of threads (don’t worry about child processes for now) and I don’t have any experience with synchronous signals, let alone in a POSIX threads application. I have used signal() before but apparently it’s non-portable and it doesn’t do what I need anyway. I have no experience at all with sigaction methods, and I can’t find good documentation on how to fill in the structs and so on.

What I need to do is to synchronously catch terminating signals like SIGINT, SIGTERM and SIGQUIT in the above loop (and I need to ignore SIGPIPE altogether so that I can catch the EPIPE error from IO methods), so it would look like this:

...
while (1)
{
    checkDatabase();
    checkChildren();
    checkForSignals();

    sleep(3);
}
...

All other threads should not have anything to do with the signal; only the thread that executes this loop should be aware of it. And, obviously, it needs to be a non-blocking check so the loop doesn’t block during its first iteration. The method called if a signal is found will sort out the other threads and destroy mutexes, and all that.

Could anyone please give me a heads-up? Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:32:06+00:00Added an answer on May 28, 2026 at 3:32 am

    (Following the question’s comments, and for completeness, this solution tries to avoid signal handlers.)

    It is possible to block signals from being raised through sigprocmask() (or, rather, pthread_sigmask() since you’re using threads). From there on, the signals that were raised but blocked are available through sigpending().

    Therefore, you could do something like (error checking omitted for brevity):

    sigset_t blocked;
    sigemptyset(&blocked);
    sigaddset(&blocked, SIGINT);
    sigaddset(&blocked, SIGTERM);
    sigaddset(&blocked, SIGQUIT);
    pthread_sigmask(SIG_BLOCK, &blocked, NULL); // Block SIGINT, SIGTERM and SIGQUIT.
    signal(SIGPIPE, SIG_IGN);                   // Ignore SIGPIPE.
    

    Then, later:

    void checkForSignals(void)
    {
        sigset_t pending;
        sigpending(&pending);
        if (sigismember(&pending, SIGINT)) {
            // Handle SIGINT...
        }
        if (sigismember(&pending, SIGTERM)) {
            // Handle SIGTERM...
        }
        if (sigismember(&pending, SIGQUIT)) {
            // Handle SIGQUIT...
        }
    }
    

    Since sigpending() does not block, this seems to match your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has two threads. The first one (the main thread)
There is a thread in my Delphi application that has a message-waiting loop. Every
I have a GUI C# application that has a single button Start/Stop. Originally this
I have an network application which uses a select loop like this: bool shutdown=false;
I have a Delphi 6 application that has a thread dedicated to communicating with
I have a thread in my application that is running code that can potentially
Is tight looping in a program bad? I have an application that has two
I have an application which has a loop, part of a Scheduler, which runs
I have a thread that sits in a blocking recv() loop and I want
I have a thread that, when its function exits its loop (the exit is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.