c - Starting a thread from inside an interrupt handler -
i'm trying start thread interrupt occurs. however, have realized can't start thread within interrupt handler (or function directly or indirectly being called interrupt handler). so, have decided have handler assert flag. then, separate thread continously monitors flag , if it's asserted in turn create (and start) thread. here's pseudocode:
int interrupt_flag = 0; interrupt_handler(void) { interrupt_flag = 1 } monitoring_thread(void) //this thread started @ start of program { while(1) { if(interrupt_flag) { interrupt_flag = 0; //start thread here sleep(/*some amount of time*/); } } }
i'm not happy having dedicated while loop monitoring flag. problem reduces speed of other threads in program. reason, i'm calling sleep function increase speed of other threads in program.
question: there way can start thread upon interrupt, without having dedicated while loop? there workaround starting thread within interrupt handler?
if makes difference, i'm using posix library.
thanks,
ps. question related earlier question posted here:
sharing data between master thread , slave thread in interrupt driven environment in c
instead of having monitoring thread spin on flag, wait until interrupt handler provides notification thread should spawned. 1 way semaphore:
sem_t interrupt_sem; void interrupt_handler(void) { sem_post(&interrupt_sem); } void monitoring_thread(void) { while(1) { sem_wait(&interrupt_sem); //start thread here } }
previously, had solution based on condition variable, unlikely system operate correctly if interrupt handler makes blocking calls. cause deadlock or other undefined behaviors, variables in system may not have consistent values @ time interrupt takes place.
as pointed out in comments myself , others, operating system should provide kind of interface explicitly wake waiting task. in code above, assuming monitoring thread active in background.
Comments
Post a Comment