Mailboxes
4-68
Example 4.15. MBX Example With Two Types of Tasks (continued)
After the program runs, review the trace log contents. The results should be
similar to that shown in Figure 4-15.
/*
* ======== reader ========
*/
Void reader(Void)
{
MsgObj msg;
Int i;
for (i=0; ;i++) {
/* wait for mailbox to be posted by writer() */
if (MBX_pend(&mbx, &msg, TIMEOUT) == 0) {
LOG_printf(&trace, "timeout expired for MBX_pend()");
break;
}
/* print value */
LOG_printf(&trace, "read ’%c’ from (%d).", msg.val, msg.id);
}
LOG_printf(&trace, "reader done.");
}
/*
* ======== writer ========
*/
Void writer(Int id)
{
MsgObj msg;
Int i;
for (i=0; i < NUMMSGS; i++) {
/* fill in value */
msg.id = id;
msg.val = i % NUMMSGS + (Int)(‘a’);
LOG_printf(&trace, "(%d) writing ‘%c’ ...", id,
(Int)msg.val);
/* enqueue message */
MBX_post(&mbx, &msg, TIMEOUT);
/* what happens if you call TSK_yield() here? */
/* TSK_yield(); */
}
LOG_printf(&trace, "writer (%d) done.", id);
}