Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. #include <pthread.h>
  7.  
  8. void* smk_timeout_fn(void* data) {
  9. bool(*fn)(void) = data;
  10. return (bool*) &fn();
  11. }
  12.  
  13. void smk_xcl_timeout(useconds_t timeout, bool(*fn)(void)) {
  14. pthread_mutex_t mutex;
  15. pthread_t thid;
  16. pthread_mutex_init(&mutex, NULL);
  17. while (true)
  18. {
  19. usleep(timeout);
  20. pthread_mutex_lock(&mutex);
  21. pthread_create(&thid, NULL, smk_timeout_fn, fn);
  22. pthread_detach(thid);
  23. pthread_mutex_unlock(&mutex);
  24. }
  25. }
  26.  
  27. int smk_xcl_timeslen(useconds_t* times) {
  28. return (int) (sizeof(times) / sizeof(useconds_t));
  29. }
  30.  
  31. void smk_xcl_timeline(useconds_t* times, bool(*fn)(void)) {
  32. pthread_mutex_t mutex;
  33. pthread_t thid;
  34. pthread_mutex_init(&mutex, NULL);
  35. while (true)
  36. {
  37. for (int i = 0; i < smk_xcl_timeslen(times); i++)
  38. {
  39. usleep(timeout[i]);
  40. pthread_mutex_lock(&mutex);
  41. pthread_create(&thid, NULL, smk_timeout_fn, fn);
  42. pthread_detach(thid);
  43. pthread_mutex_unlock(&mutex);
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement