Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- void* smk_timeout_fn(void* data) {
- bool(*fn)(void) = data;
- return (bool*) &fn();
- }
- void smk_xcl_timeout(useconds_t timeout, bool(*fn)(void)) {
- pthread_mutex_t mutex;
- pthread_t thid;
- pthread_mutex_init(&mutex, NULL);
- while (true)
- {
- usleep(timeout);
- pthread_mutex_lock(&mutex);
- pthread_create(&thid, NULL, smk_timeout_fn, fn);
- pthread_detach(thid);
- pthread_mutex_unlock(&mutex);
- }
- }
- int smk_xcl_timeslen(useconds_t* times) {
- return (int) (sizeof(times) / sizeof(useconds_t));
- }
- void smk_xcl_timeline(useconds_t* times, bool(*fn)(void)) {
- pthread_mutex_t mutex;
- pthread_t thid;
- pthread_mutex_init(&mutex, NULL);
- while (true)
- {
- for (int i = 0; i < smk_xcl_timeslen(times); i++)
- {
- usleep(timeout[i]);
- pthread_mutex_lock(&mutex);
- pthread_create(&thid, NULL, smk_timeout_fn, fn);
- pthread_detach(thid);
- pthread_mutex_unlock(&mutex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement