jeudi 2 août 2018

Why recv return -1 and errno=EINTR when set SO_RCVTIMEO?

If I run t2 , recv in t1 will return -1 and errno=EINTR. But recv in t1 functions normal without t2 started, it just block 3sec. Here is code:

int sock_fd;
timeval timeout{3,0};
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeval));
connect.....

void recvThread()
{
    char pData[4096];
    int len = recv(sock_fd,pData,4096,0);
    if(len<=0)
    {
        ...
    }
}

void otherThread()
{
    while(1)
    {
         //do nothing
    }
}

std::thread t1(recvThread);
std::thread t2(otherThread);

Aucun commentaire:

Enregistrer un commentaire