multithreading - Synchronizing data across threads in C with user input -


i'm building embedded thing physical buttons. i've got different threads handle different modes thing can in, example:

void *maketoastthread() { for(;;)    if(current_mode==make_toast) makesometoast();    else sleep(50ms);  void *makecoffeethread() { for(;;)    if(current_mode==make_coffee) makesomecoffee();    else sleep(50ms);  main ()    // start threads    // poll buttons    // set current_mode (global) using mutex_lock/mutex_unlock 

each of these threads updates display when active , i'm noticing race conditions corrupting display. question what best approach avoid different threads updating display @ same time? or more broadly, how should c program handle user interrupts , sync data in different threads?

one suggestion have separate thread only 1 responsible i/o user.

then have other threads use inter-thread communications of description if want interact user.

many systems use concept , eases complexity.

an example message queue between thread wanting i/o , thread doing i/o.

for example, send output request message responsible thread content specifying print , where. carry on, safe in knowledge output done @ point.

or send input request detailing prompt , write it, maximum input size, memory location input should put , address of callback function call, or flag set, when operation has ended. complex but, atomic operation, make users of facility lot happier.


another possibility make user i/o resource each thread lock mutex when wants access it.

the thread mutex 1 allowed interact , others forced wait until it's made available again.

however, this solution may end threads being blocked when otherwise doing real work. wouldn't matter output since should relatively fast, input may become problem, since relies on user entering something.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -