Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * compile gcc main.c -lrt -o test
- */
- #include <stdio.h>
- #include <sys/time.h>
- #include <signal.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <pthread.h>
- pthread_t t;
- struct object {
- timer_t *timerid;
- struct sigevent *sev;
- struct itimerspec *its;
- int count;
- void **func;
- };
- struct thread_func {
- void (*func) ( void *data );
- void *data;
- };
- void func_hello ( void *data ) {
- printf ( "func_hello\n" );
- }
- void func_neho ( void *data ) {
- printf ( "func_neho\n" );
- }
- void func_fuck ( void *data ) {
- printf ( "func_fuck %s\n", (char *) data );
- }
- void hh ( union sigval val ) {
- struct thread_func *thf = ( struct thread_func * ) val.sival_ptr;
- thf->func ( thf->data );
- }
- void *handle ( void *data ) {
- struct object *obj = ( struct object * ) data;
- char ch = getchar ( );
- }
- struct object *obj_init ( ) {
- struct object *obj = calloc ( 1, sizeof ( struct object ) );
- obj->timerid = calloc ( 0, sizeof ( timer_t ) );
- obj->sev = calloc ( 0, sizeof ( struct sigevent ) );
- obj->its = calloc ( 0, sizeof ( struct itimerspec ) );
- obj->func = calloc ( 0, sizeof ( void * ) );
- obj->count = 0;
- return obj;
- }
- void obj_set_timer ( struct object *obj, int num, void (*func ) ( void *data ), void *data ) {
- int index = 0;
- obj->count++;
- obj->timerid = realloc ( obj->timerid, sizeof ( timer_t ) * obj->count );
- obj->sev = realloc ( obj->sev, sizeof ( struct sigevent ) * obj->count );
- obj->its = realloc ( obj->its, sizeof ( struct itimerspec ) * obj->count );
- obj->func = realloc ( obj->func, sizeof ( void * ) * obj->count );
- obj->sev[index].sigev_notify = SIGEV_THREAD;
- obj->sev[index].sigev_notify_function = hh;
- obj->sev[index].sigev_signo = SIGALRM;
- struct thread_func *thf = calloc ( 1, sizeof ( struct thread_func ) );
- thf->func = func;
- thf->data = data;
- obj->sev[index].sigev_value.sival_ptr = thf;
- if ( timer_create ( CLOCK_REALTIME, &obj->sev[index], &obj->timerid[index] ) == -1 ) fprintf ( stderr, "error timer_create.\n" );
- obj->its[index].it_value.tv_sec = num;
- obj->its[index].it_value.tv_nsec = 0;
- obj->its[index].it_interval.tv_sec = num;
- obj->its[index].it_interval.tv_nsec = 0;
- if ( timer_settime ( obj->timerid[index], 0, &obj->its[index], NULL ) == -1 ) fprintf ( stderr, "error timer settimer.\n" );
- }
- int main ( ) {
- struct object *obj = obj_init ( );
- obj_set_timer ( obj, 1, func_hello, NULL );
- obj_set_timer ( obj, 4, func_neho, NULL );
- obj_set_timer ( obj, 6, func_fuck, "test" );
- char ch = getchar ( );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement