简体   繁体   中英

C socket, first thread connected close the remaining

A client can connect contemporary to n couples of servers. As soon as the first of these is successful, it must send a signal to all the others, to close the remaining threads that are trying to connect. Shound I use joinables threads to do this? If yes, at least conceptually, how should I proceed?

Thanks to all

We consider a situation where n resources are requested, and the first one obtained cancels all the other requests. In your specific situation, resources are socket connections.

If the resources can be requested asynchronously ( future/promise ), then there must be a protocol to

  • either check regularly on the status of the request (polling), or receive a notification of the completion (handler) of that request, and

  • possibly cancel the request

In that model, you only need one thread to launch all the requests and poll/wait for notifications. Once one or more resources are acquired, select one of them, close the others, and cancel all the remaining demands (if cancellation is not possible, simply release each extra resource at notification time, or dedicate an extra thread to poll on them and release when received).

In case where the resource request command is synchronous (a blocking socket connect call), you will need indeed n concurrent threads, each calling the blocking function. At return time, each of them should check on a flag indicating whether another thread has obtained his resource first. If it is not the case, the thread position the flag and carry on. Otherwise, the thread release the resource. The flag must be atomic, or mutex protected.

As an analogy, imagine a race between n contestants: they all start one after the other, and there's a flag they must capture to be allowed to continue. In order to do so, they must go in a room which only allow 1 person at a time (the mutex), but first they have to get a number (connect to a server). Once a contestant succeeded at getting a number, she goes in front of the door and try to enter the room (hold the mutex). If nobody's inside, she enters (or she waits until the room is empty), and check if the flag is still there ( flag == 0 ). If it is, she takes it ( flag = 1; ), and proceed. Otherwise, she must give back her number and exit the race (close connection and exit).

The thread task is basically as follow:

void task(address){
    int socket = connect(address);
    If (!check_flag()){
        proceed(socket);
    } else {
        close(socket);
    }
}

Where proceed is the remaining of the work with the socket. The check_flag function is explained below.

Using pthreads , you have pthread_mutex to implement it. If the machine memory model allows it, you may also use a simple volatile integer, but it is clearly non portable.

Fundamentally, only one function is needed to handle the flag. The pseudo code would look like this:

int flag;

int check_flag(){
    int result=0;
    lock();
    result = flag;
    flag = 1;
    unlock(); 
    return result;
}

It returns whether the flag is already set, and set it as well.

I should add that provides optional support for atomic variables . See your OS C compiler documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM