Two threads one keyboard ?


5 posts
by fa001 » Fri Feb 01, 2013 5:38 pm
I want to allow for selection of next song while one song is playing. So I was thinking of putting the music player in one thread and the selection routine in another thread. However both can use keyboard input. So how does one direct keyboard input to a specific selected thread?
Posts: 14
Joined: Wed Jan 02, 2013 8:25 pm
by -rst- » Mon Feb 04, 2013 5:42 pm
Third thread for handling the keyboard and sending commands to the other two depending on the input?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 898
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland
by fa001 » Tue Feb 05, 2013 5:22 pm
Thanks for the reply.
The third thread is a good idea however my question is how does one direct input to a specific thread?
I am a bit lost in pipes and FIFOs so I was looking for something easy :)
Posts: 14
Joined: Wed Jan 02, 2013 8:25 pm
by -rst- » Tue Feb 05, 2013 5:57 pm
So you are planning to incorporate an external 'ready-made' player as part of your system? And that player has some keyboard control? And you want to implement a song selection mechanism, which would grab keyboard input and not let it go to the player? Hmm... that might be a bit more complicated - depending on the player...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 898
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland
by rurwin » Tue Feb 05, 2013 6:07 pm
Probably your best plan would be for one or other of the threads to be responsible for the keyboard, and if they get a key they are not interested in they pass it to the other thread. Whether you use pipes for that is up to you, but it is probably the easiest way; multi-threading contention is a complex subject.

A simple solution would be to wrap the keyboard routines in a mutex, so only one thread could be in there at a time. The player thread might be in and out quickly, whereas the selection thread could keep it locked while the selection was happening. During which time the player thread repeatedly asks for the mutex, but times out and goes about its other business.
User avatar
Forum Moderator
Forum Moderator
Posts: 2904
Joined: Mon Jan 09, 2012 3:16 pm