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

  • SEARCH
  • Home
  • 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 8817767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:55:53+00:00 2026-06-14T04:55:53+00:00

Possible Duplicate: Wake up thread blocked on accept() call I am writing a small

  • 0

Possible Duplicate:
Wake up thread blocked on accept() call

I am writing a small server which listening for connections (accepting them and passing them to worker threads) until a custom stop signal is sent.

If I use blocking sockets, then my main accept loop cannot break on the custom stop signal being sent. However, I wish to avoid having a busy-wait/spinlock loop with a non-blocking socket.

What I want is for my main accept loop to block until either a connection is received or the stop signal is sent.

Is this possible in C on Linux?

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-06-14T04:55:54+00:00Added an answer on June 14, 2026 at 4:55 am

    If I understand correctly then you are open to using any kind of “signal”, not necessarily a POSIX signal. Actually, POSIX signals are a poor choice because checking if you have received one in a loop has unavoidable race conditions.

    What you need to use is anything that can be monitored through a file descriptor. It could be:

    • A pipe. Your accept loop monitors the read end of the pipe. To wake up the loop, another thread writes something into (doesn’t matter what) into the write end.
    • Another socket. Similarily, another thread wakes up your accept loop by writing into the other end of the socket.
    • A file in the filesystem which you monitory using inotify.
    • A device which receives some data when the loop should be interrupted.
    • etc…

    The later entries in the list of examples aren’t generally practical. They’re just to illustrate that it can be any type of object as long as it is has monitorable file descriptor. The simplest, cheapest, and most popular way is a pipe.

    If you are already using nonblocking sockets, then you certainly already have some kind of polling loop to check when they’re ready to accept connections. I’m going to assume you’re using poll() to do this.

    Before you start your loop, set up a pipe like this:

    pipe(&pipefd[0]);
    read_end = pipefd[0];
    write_end = pipefd[1];
    fcntl(read_end, F_SETFL, O_NONBLOCK);
    

    The fcntl is to set the read end of the pipe to non blocking mode. You’re going to be using that end of the pipe in your poll call together with your socket, so it needs to be nonblocking.

    Then just add the read end of the pipe to the list of tile descriptors that you monitor in your accept loop:

    for (;;) { /* accept loop, loop forever */
        /* Monitor your socket. You should already have this in your code */
        pollfd[0].fd = your_socket;
        pollfd[1].events = POLLIN;
    
        /* Add a new entry to monitor the read end of the pipe */
        pollfd[1].fd = read_end;
        pollfd[1].events = POLLIN;
    
        /* Now call poll, as before, but monitor 2 file descriptors instead of just 1 */
        n = poll(&pollfd[0], 2, -1);
    
        /* Check if your socket is ready to accept.
           This part, you are already doing. */
        if (pollfd[0].revents) {
            newsocket = accept(your_socket, etc....)
            do_somehting_with(new_socket);
        }
    
        /* New part: check if anyone has written something for you into your pipe */
        if (pollfd[1].revents) {
            break; /* stop the loop */
        }
    }
    

    The example uses poll but you might be using select (or even epoll) instead. select has a different interface but the idea is the same.

    This technique is known as the self-pipe trick.

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

Sidebar

Related Questions

Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: Transparent images with C# WinForms I am coding an app which will
Possible Duplicate: Javascript window resize event I want to get an event which is
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP

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.