This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Which is better?global variable or message box

I want to develop a firmware for an embedded system (with LPC1313 micro), I want to use RTX RTOS for this project. I write the firmware myself(single developer).

I have seven tasks that should communicate with each other,some times one task send and receive data from many tasks.

What is the better way for task communications ? using global variables with some locking mechanism or message boxes?

If I want to use message box ,Is it needed to define one mbox for each communication between 2 tasks?

please help me.

  • Everything always depends on the specific requirements - there are almost never any "right" answer.

    The good thing with message boxes are that:
    - the thread can sleep until there is data available for processing.
    - the mailbox construct takes care of the synchronization, avoiding problems with incorrect accesses because of lack of synchronization or lock primitives.
    - the mailbox construct can have the "producer" see that it can't send more data until the "consumer" have been busy enough processing data so there are free "envelopes" available for new mails.

    You can have a common mailbox pool. What you have to consider, is number of mails you need to have concurrently in existence. And if there is potential for one function in the program consuming all mails, starving all other logic.

    Note that mailboxes aren't the only interprocess communication/synchronization method available. In many situations, events will be enough. The only message is basically a flag.

    Global variables are best if multiple tasks needs to access the same information. Then the individual tasks can pick up a lock, access the data, and then release the lock. But it isn't really a good mechanism for sending information from thread A to thread B.

  • So for communication between two tasks we use mailbox ,but for sharing some information between several task we use global variables and use event and lock mechanism for sync and access contol.