Jae Park
Jae Park

Reputation: 641

How to Break C++ Accept Function?

When doing socket programming, with multi-threading,

if a thread is blocked on Accept Function,

and main thread is trying to shut down the process,

how to break the accept function in order to pthread_join safely?

I have vague memory of how to do this by connection itself to its own port in order to break the accept function.

Any solution will be thankful.

Cheers

Upvotes: 7

Views: 6876

Answers (6)

Rahim Khoja
Rahim Khoja

Reputation: 735

If available I would use a select function with a timeout in a loop to achieve this functionality.

as Glenn suggested

The select function with a timeout value will wait for a socket to connect for a set period of time. If a socket attempts to connect it can be accepted during that period. By looping this select with a timeout it is possible to check for new connections until the break condition is met.

Here is an example:

std::atomic<bool> stopThread;
void theThread ( std::atomic<bool> & quit )
{
    struct timeval tv;
    int activity;
    ...
    while(!quit) 
    {
        // reset the time value for select timeout
        tv.tv_sec = 0;
        tv.tv_usec = 1000000;
        ...

        //wait for an activity on one of the sockets
        activity = select( max_sd + 1 , &readfds , NULL , NULL , &tv);

        if ((activity < 0) && (errno!=EINTR)) 
        {
            printf("select error");
        }

        if (FD_ISSET(master_socket, &readfds)) 
        {
            if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
            {
                perror("accept");
                exit(EXIT_FAILURE);
            }
       ...
    }
}

int main(int argc, char** argv)
{
...
stopThread = false;
std::thread foo(theThread, std::ref(stopThread));
...    

stopThread = true;
foo.join();
return 0;

}

A more complete example of 'Select' http://www.binarytides.com

I am pretty new to C++ so I am sure my code and answer can be improved.

Upvotes: 1

Glenn
Glenn

Reputation: 1167

Depending on your system, if it is available, I would use a select function to wait for the server socket to have a read, indicating a socket is trying to connect. The amount of time to time to wait for a connection can be set/adjusted to to what every time you want to wait for a client to connect(infinity, to seconds, to 0 which will just check and return). The return status needs to be checked to see if the time limit was reached (no socket is trying to connect), or if there is something waiting to be serviced (your server socket indicating there is a client which would like to connect). You can then execute the accept knowing there is a socket to connect based on the returned status.

Upvotes: 1

Martin James
Martin James

Reputation: 24857

Some choices:

a) Use non-blocking

b) Use AcceptEx() to wait on an extra signal, (Windows)

c) Close the listening socket from another thread to make Accept() return with an error/exception.

d) Open a temporary local connection from another thread to make Accept() return with the temp connection

Upvotes: 3

std&#39;&#39;OrgnlDave
std&#39;&#39;OrgnlDave

Reputation: 3968

Typically if you want to do multi-threaded networking, you would spawn a thread once a connection is made (or ready to be made). If you want to lower the overhead, a thread pool isn't too hard to implement.

Upvotes: -2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153840

The typical approach to this is not to use accept() unless there is something to accept! The way to do this is to poll() the corresponding socket with a suitable time-out in a loop. The loop checks if it is meant to exit because a suitably synchronized flag was set.

An alternative is to send the blocked thread a signal, e.g., using pthread_kill(). This gets out of the blocked accept() with a suitable error indication. Again, the next step is to check some flag to see if the thread is meant to exit. My preference is the first approach, though.

Upvotes: 1

Seg Fault
Seg Fault

Reputation: 1351

Sounds like what you are looking for is this: You set a special flag variable known to the listening/accepting socket, and then let the main thread open a connection to the listening/accepting socket. The listening/accepting socket/thread has to check the flag every time it accepts a connection in order to know when to shut down.

Upvotes: 0

Related Questions