0

What is wrong with this code? When the sem object is posted, it is fine. However as soon as it needs to wait for the time out, I get "The futex facility returned an unexpected error code".

void* BackgroudProc(void*){
    struct timespec ts;
    while(1){
        clock_gettime(CLOCK_REALTIME, &ts);
        ts.tv_sec += 10;
        sem_timedwait(&hFCRequestEvt, &ts);
        for(int i = 0; i < requests.size(); i++){
            if(numOfConnection >= MAX_CONNECTION)
                break;
            if(requests[i].state == QUEUED){
                requests[i].state = STARTED;
                numOfConnection++;
                pthread_create(&requests[i].tid, 0, (void* (*)(void*))FileCopyFSM, (void*)(&requests[i]));
            }
        }
    }
}
0x64
  • 124
  • 8
  • 1
    Assuming you get the error from `sem_timedwait(&hFCRequestEvt, &ts);`, how do you know you get an error? The posted code isn't checking any return values. – Andrew Henle Mar 14 '18 at 18:35
  • It returns -1 and perror prints "Connection timed out". Then immediately I get "The futex facility returned an unexpected error code.Aborted (core dumped)" – 0x64 Mar 14 '18 at 18:49
  • 1
    *It returns -1 and perror prints "Connection timed out".* What call to `perror()`? Your posted code doesn't match what you're describing? I also wonder how you avoid race conditions in accessing the members of `requests` - you're not doing any locking to access the `state`, and presumably other threads can alter that value. – Andrew Henle Mar 14 '18 at 19:21
  • I added code on my end for the return value and perror() for debugging. They are not in the original question. Sorry for the confusion.The requests are irrelevant here. The STARTED state just marks the beginning of the requests processing. There is no race condition. – 0x64 Mar 14 '18 at 19:39
  • After I wrote more code for the other parts of the program, as mysterious as the problem appeared, it now disappeared. sem_timedwait now works as expected even though I've made no change to the posted code, which is pretty weird because I am sure the futex facility error originated from the thread that is doing sem_timedwait. – 0x64 Mar 15 '18 at 00:32
  • please add a 'EDIT' section to your question, with the updated code. Other wise we are just guessing. Also, post a [mcve] so we can see what your code is actually doing. – user3629249 Mar 15 '18 at 00:55

0 Answers0