6

Here i have one function which is listen mode. this function listing something which i got form some device.

Here when my function is in listen mode that time i want to create timeout. if i will not get any response from particular device than i want o exit from this function and have to notify.

if during this timeout period if i will get response from device than i have to continue with work and stop this timeout and there is no limits to complete this work in any time duration.

So how can i implement this thing for a function.

Any body please can me help me to implement this thing with timeout functionality.

1
  • I think the only sane way to do this is to start a separate processes, which you then kill if it takes too long. It is very hard to "stop" code that is running inside your process. Commented Jan 31, 2012 at 4:30

2 Answers 2

5

Depending on how you are waiting for a response from this device, the answer to your question will be different. The basic framework is:

int do_something_with_device()
{
    if (!wait_for_response_from_device()) {
        return TIMEOUT_ERROR;
    }
    // continue with processing
}

As for how you implement wait_for_response_from_device(), well, every device is different. If you're using sockets or pipes, use select(). If you're interfacing with something that requires a busy-wait loop, it might look like:

int wait_for_response_from_device()
{
    time_t start = time(NULL);
    while (time(NULL) - start < TIMEOUT) {
        if (check_device_ready()) {
            return 1;
        }
    }
    return 0;
}

Naturally, the implementation of check_device_ready() would be up to you.

Sign up to request clarification or add additional context in comments.

2 Comments

Here i have one function which put my device in listen mode , because my device is ready to receive file from any device through bluetooth. here i am using obex_test library for this
After some time if i am not getting response form remote device means (any mobile device) than i have to disconnect this and notify my server
3

Take a look at man 2 alarm. You can set or disable signals which will be sent to your application after a certain time period elapses.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.