dispatch_timeout()

QNX SDP8.0C Library ReferenceAPIDeveloper

Set a timeout

Synopsis:

#include <sys/iofunc.h>
#include <sys/dispatch.h>

int dispatch_timeout( dispatch_t *dpp,
                      struct timespec *reltime );

Arguments:

dpp
A dispatch handle created by a successful call to dispatch_create().
reltime
A pointer to a timespec structure that specifies the relative time of the timeout, or NULL to cancel the timeout.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The function dispatch_timeout() sets a timeout that's used when blocking with dispatch_block().

CAUTION:
Applying timeouts in a multi-threaded resource manager may not have the expected effect. Some, but not all, threads may be servicing many client requests while another thread is completely inactive. If a regular wake-up is needed for sanity or health checking, a timer delivering a pulse might be a better choice.

This function is part of the dispatch layer of a resource manager. For more information, see Layers in a resource manager in the Bones of a Resource Manager chapter of Writing a Resource Manager.

This affects all future calls to dispatch_block() with the same dispatch handle, unlike the similar TimerTimeout() and timer_timeout() calls, which affect only the next blocking call.

To handle timeouts, use message_attach() with MSG_FLAG_TIMEOUT_FUNC to register a callback.

Returns:

0
Success.
-1
An error occurred.

Examples:

#include <sys/dispatch.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
timeout_handler(message_context_t *ctp, int code,
                unsigned flags, void *handle ) {
    printf("dispatch block was timedout");
    return 0;
}

int main( int argc, char **argv ) {
   dispatch_t *dpp;
   struct timespec time_out;
   int timed_out;
   time_out.tv_sec = 1;
   time_out.tv_nsec = 2;
 message_attr_t msg_attr = {.flags = MSG_FLAG_TIMEOUT_FUNC };

   if( ( dpp = dispatch_create() ) == NULL ) {
     fprintf( stderr, "%s: Unable to allocate \
              dispatch handle.\n",argv[0] );
     return EXIT_FAILURE;
   }
  ...
 /*
   Register a handler for timeouts.
 */
 if(message_attach(dpp, &msg_attr, 0, 0,
                      &timeout_handler, NULL) == -1) {
        fprintf(stderr,
           "Unable to attach a timeout handler.\n");
        return EXIT_FAILURE;
    }

   …

   if ( (timed_out = dispatch_timeout ( dpp, &time_out ))
         == -1 ) {
       fprintf ( stderr, "Couldn't set timeout );
       return EXIT_FAILURE;
   }
   /* else successful timeout set */

   …
   return EXIT_SUCCESS;
}

For examples using the dispatch interface, see dispatch_create(), message_attach(), resmgr_attach(), and thread_pool_create().

Classification:

QNX OS

Safety:
Cancellation pointNo
Signal handlerNo
ThreadYes
Page updated: