fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct handler_t handler_t;
  4. typedef void (*handle_func)(handler_t*);
  5. typedef int timer_handle_t;
  6.  
  7. struct handler_t {
  8. char* id;
  9. handle_func execute;
  10. timer_handle_t timer_handle;
  11. unsigned int duration;
  12. };
  13.  
  14. static void func1(handler_t* handler);
  15. static void func2(handler_t* handler);
  16. static void start_timer(timer_handle_t timer_handle, unsigned int duration);
  17.  
  18. static const timer_handle_t timer1 = 1;
  19. static const timer_handle_t timer2 = 2;
  20. static handler_t handler1 = {"hndl1", func1, timer1, 5};
  21. static handler_t handler2 = {"hndl2", func2, timer2, 10};
  22.  
  23. static void start_timer(timer_handle_t timer_handle, unsigned int duration) {
  24. printf("starting timer %d with duration %u\n", timer_handle, duration);
  25. }
  26.  
  27. static void func1(handler_t* handler) {
  28. printf("Handler %s executing function\n", handler->id);
  29. if (5 > 3) { // Some condition that is different from func2
  30. start_timer(handler->timer_handle, handler->duration);
  31. }
  32. }
  33.  
  34. static void func2(handler_t* handler) {
  35. printf("Handler %s executing function\n", handler->id);
  36. if (1 < 4) { // Some condition that is different from func1
  37. start_timer(handler->timer_handle, handler->duration);
  38. }
  39. }
  40.  
  41. int main(void) {
  42. handler_t* handlers[2] = {&handler1, &handler2};
  43.  
  44. for (unsigned int i=0; i<2; ++i) {
  45. handlers[i]->execute(handlers[i]);
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5552KB
stdin
Standard input is empty
stdout
Handler hndl1 executing function
starting timer 1 with duration 5
Handler hndl2 executing function
starting timer 2 with duration 10