Allocate a dispatch handle
#include <sys/dispatch.h> dispatch_t *dispatch_create( void );
libc
The dispatch_create() function allocates and initializes a dispatch handle. The attach functions are:
If you wish, you can do a resmgr_attach() with a NULL path. This has the effect of initializing dispatch to receive messages and creates the channel among other things.
![]() |
A channel is created only when you first attach something that requires a channel (indicating you will block receiving messages). |
A handle to a dispatch structure. If an error occurs, the function returns NULL.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
int my_func( select_context_t *ctp, int fd,
unsigned flags, void *handle ) {
int i, c;
/* Do some useful stuff with data */
i = read( fd, &c, 1 );
fprintf( stderr, "activity on fd %d: read char %c,
return code %d\n", fd, c, i );
}
int main( int argc, char **argv ) {
dispatch_t *dpp;
dispatch_context_t *ctp;
select_attr_t attr;
int fd, fd2;
if( ( dpp = dispatch_create() ) == NULL ) {
fprintf( stderr, "%s: Unable to allocate \
dispatch handle.\n",argv[0] );
return EXIT_FAILURE;
}
if( argc <= 2 || (fd = open( argv[1],
O_RDWR | O_NONBLOCK )) == -1 ) {
return EXIT_FAILURE;
}
if( argc <= 2 || (fd2 = open( argv[2],
O_RDWR | O_NONBLOCK )) == -1 ) {
return EXIT_FAILURE;
}
select_attach( dpp, &attr, fd,
SELECT_FLAG_READ | SELECT_FLAG_REARM, my_func, NULL );
select_attach( dpp, &attr, fd2,
SELECT_FLAG_READ | SELECT_FLAG_REARM, my_func, NULL );
ctp = dispatch_context_alloc( dpp );
for(;;) {
if( ctp = dispatch_block( ctp ) ) {
dispatch_handler( ctp );
}
}
return EXIT_SUCCESS;
}
For more examples using the dispatch interface, see message_attach(), resmgr_attach(), and thread_pool_create().
| Safety: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |
dispatch_block(), dispatch_context_alloc(), dispatch_destroy(), dispatch_handler(), dispatch_timeout(), message_attach(), pulse_attach(), resmgr_attach(), select_attach()