XK-1A Development Board Tutorial
5.3
Communicate between threads
A
channel
provides a synchronous, bidirectional link between two threads. It
consists of two channel ends, which two threads can use to interact on demand
using the XC input and output statements.
The program below creates two threads which are connected using a channel, and
communicates a value over this channel.
v o i d snd ( c h a n e n d c o u t ) {
c o u t <: 1;
}
v o i d rcv ( c h a n e n d cin ) {
int x ;
cin : > x ;
}
int m a i n ( v o i d ) {
c h a n c ;
par {
snd ( c ) ;
rcv ( c ) ;
}
r e t u r n 0;
}
The function
snd
declares a channel end parameter
cout
, which refers to one end
of a channel. It uses the XC output statement to output the value 1 to a receiving
thread.
The function
rcv
declares a channel end parameter
cin
and uses the XC input
statement to input a value to the local variable
x
.
The function
main
declares a channel
c
. The locations of its two channel ends are
established through its use in two statements of the
par
.
5.4
Exercise 1
To complete this part of the tutorial, perform the following tasks:
1. Modify your application so that after pressing and releasing button BUT1, the
flashing LED changes direction. This requires the function
respondToButton
to
communicate with the function
flashLEDs4bitPort
.
Explain the solution in more detail..
Follow these steps:
2. In the function
respondToButton
:
·
Add a parameter declaration
chanend c
.
X7366A