How to break C ++ Accept function?

When programming multithreaded sockets

if the thread is blocked in Accept Function,

and the main thread is trying to complete the process,

how to break accept function for pthread_join safely?

I have vague memory on how to do this, connecting myself to my own port in order to break the accept function.

Any decision would be appreciated.

Greetings

+4
source share
6 answers

Some options:

a) Use non-blocking

b) Use AcceptEx () to wait for an additional signal (Windows)

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

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

+2
source

A typical approach to this is not to use accept() if there is something to accept! The way to do this is to poll() corresponding socket with a suitable timeout in the loop. The loop checks to see if it should exit because a flag was set with the synchronization set.

An alternative is to send a blocked signal stream, for example, using pthread_kill() . This exits the blocked accept() with an appropriate error indication. Again, the next step is to check some flag to see if the stream should exit. My preference is the first approach.

+1
source

Depending on your system, if available, I would use the select function to wait for the server socket to receive a read, indicating that the socket is trying to connect. The amount of timeout for the connection can be set / adjusted so that every time you want to wait for the client to connect (infinity, seconds, to 0, which will simply check and return). You need to check the return status to see if the time limit has been reached (no socket is trying to connect), or if something is waiting for service (your server socket indicates that there is a client that would like to connect). You can then execute accept , knowing that there is a socket to connect based on the returned state.

+1
source

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

as suggested by Glenn

The select function with a timeout value will wait for the socket to connect for a specified period of time. If a socket tries to connect, it can be accepted during this period. By cycling this choice with a timeout, you can test new connections until the interrupt 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 Select Example http://www.binarytides.com

I'm new to C ++, so I'm sure my code and answer can be improved.

+1
source

It looks like what you are looking for is this: you set a special flag variable known to the socket for listening / receiving, and then let the main thread open the connection to the listening / receiving socket. The listening / receiving socket / stream should check the flag every time it accepts the connection in order to know when to close it.

0
source

Generally, if you want to create multi-threaded networks, you should create a stream as soon as a connection is created (or ready to be created). If you want to reduce overhead, the thread pool is not too difficult to implement.

-2
source

Source: https://habr.com/ru/post/1435910/


All Articles